У меня есть приложение, которое может отправлять уведомления пользователям, но поскольку новые обновления PlayStore должны соответствовать targetSdkVersion 26, уведомления больше не работают с всплывающей подсказкой об ошибке Не удалось отправить уведомление на канал «null». Об этом есть несколько тем, но я не получил решения. Вот мой настоящий класс FirebaseMessagingService:
public class FirebaseMessagingService extends com.google.firebase.messaging.FirebaseMessagingService {
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
String notification_title = remoteMessage.getNotification().getTitle();
String notification_message = remoteMessage.getNotification().getBody();
String click_action = remoteMessage.getNotification().getClickAction();
String from_user_id = remoteMessage.getData().get("from_user_id");
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(notification_title)
.setContentText(notification_message);
Intent resultIntent = new Intent(click_action);
resultIntent.putExtra("user_id", from_user_id);
PendingIntent resultPendingIntent =
PendingIntent.getActivity(
this,
0,
resultIntent,
PendingIntent.FLAG_UPDATE_CURRENT
);
mBuilder.setContentIntent(resultPendingIntent);
int mNotificationId = (int) System.currentTimeMillis();
NotificationManager mNotifyMgr =
(NotificationManager) getSystemService(NOTIFICATION_SERVICE);
mNotifyMgr.notify(mNotificationId, mBuilder.build());
}
}
Что мне нужно изменить, чтобы он снова заработал? Было бы хорошо, если бы он работал и на устройствах от sdk 24 и выше.




Начиная с Android Oreo, вам нужен канал для отправки уведомлений. Вы можете сделать что-то вроде этого (не проверено):
public class FirebaseMessagingService extends com.google.firebase.messaging.FirebaseMessagingService {
public static final String NOTIFICATION_CHANNEL_ID = "10001";
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
String notification_title = remoteMessage.getNotification().getTitle();
String notification_message = remoteMessage.getNotification().getBody();
String click_action = remoteMessage.getNotification().getClickAction();
String from_user_id = remoteMessage.getData().get("from_user_id");
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(notification_title)
.setContentText(notification_message);
Intent resultIntent = new Intent(click_action);
resultIntent.putExtra("user_id", from_user_id);
PendingIntent resultPendingIntent =
PendingIntent.getActivity(
this,
0,
resultIntent,
PendingIntent.FLAG_UPDATE_CURRENT
);
mBuilder.setContentIntent(resultPendingIntent);
int mNotificationId = (int) System.currentTimeMillis();
NotificationManager mNotifyMgr =
(NotificationManager) getSystemService(NOTIFICATION_SERVICE);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O)
{
int importance = NotificationManager.IMPORTANCE_HIGH;
NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, "NOTIFICATION_CHANNEL_NAME", importance);
mBuilder.setChannelId(NOTIFICATION_CHANNEL_ID);
mNotifyMgr.createNotificationChannel(notificationChannel);
}
mNotifyMgr.notify(mNotificationId, mBuilder.build());
}
}
Вы используете последний AppCompat? Или хотя бы 26.0.2?
Работает, когда приложение открыто. если приложение закрыто или работает в фоновом режиме, снова появляется сообщение об ошибке Не удалось опубликовать уведомление на канале «null».
nmv Ive обновил обмен сообщениями firebase до 11.8.0, и теперь он работает. Благодарность
Окей круто. Если мой ответ вам подходит, не стесняйтесь голосовать за него: он может помочь кому-то другому в будущем;)
Я сделал, но: голоса, отданные теми, у кого репутация менее 15, записываются, но не меняют общедоступный рейтинг публикации.
Он работает, когда приложение находится в фоновом режиме, но получает сообщение «Не удалось опубликовать сообщение на канале« null », когда приложение открыто.
mBuilder.setChannelId (NOTIFICATION_CHANNEL_ID); вызывает Невозможно разрешить метод 'setChannelId (java.lang.String)'