Я создал функцию уведомления для приложения чата с firebase. Уведомления отправляются при получении нового сообщения, когда приложение работает в фоновом режиме. Устройство получает уведомления, но они не отображаются в строке состояния. В истории уведомлений они перечислены под полем «отклонено». Я предполагаю, что телефон как-то автоматически их отклоняет. У тебя есть идея? Может для этого нужны дополнительные разрешения? Я действительно не могу понять.
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
Log.d(TAG, "onMessageReceived: " + remoteMessage);
String channelId = "default_channel_id";
String channelDescription = "Default Channel";
AppComponent appComponent = ((AppComponentSupplier) getApplicationContext()).getAppComponent();
ChatRepository chatRepository = appComponent.chatRepository();
if (chatRepository == null) {
return;
}
String sender = remoteMessage.getData().get("sender");
if (sender == null) {
Log.e(TAG, "onMessageReceived: There was no sender in the received message");
return;
}
Disposable disposable = chatRepository.updateOnce(sender)
.subscribe(
result -> Log.d(TAG, String.format("Received %d message %s", result, sender)),
err -> Log.e(TAG, "Error during update: ", err));
// Since android Oreo notification channel is needed.
//Check if notification channel exists and if not create one
NotificationManager mNotifyMgr =
(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
NotificationChannel notificationChannel = mNotifyMgr.getNotificationChannel(channelId);
if (notificationChannel == null) {
int importance = NotificationManager.IMPORTANCE_HIGH; //Set the importance level
notificationChannel = new NotificationChannel(channelId, channelDescription, importance);
mNotifyMgr.createNotificationChannel(notificationChannel);
}
}
Intent intent = new Intent(this, ChatFragment.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this, channelId)
.setSmallIcon(R.drawable.ic_stat_ic_notification)
.setContentTitle(remoteMessage.getData().get("sender"))
.setContentText(remoteMessage.getData().get("data"))
.setChannelId(channelId)
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setContentIntent(pendingIntent);
int mNotificationId = (int) System.currentTimeMillis();
mNotifyMgr.notify(mNotificationId, mBuilder.build());
}
Изменить: использование Android Orea с эмуляцией Google Pixel 2.