#java #android #android_notification
Столкнулся с проблемой отображения нотификаций в android lolipop. При открытом приложении
они в статус бар уходят, но если приложение закрыть и удалить из "Недавние запущенные",
то отображаться перестают. На 4-х версиях андроида все работает исправно, нотификации
отображаются и при оффнутом приложении, и после перезапуска девайса. Вот метод для
отображения
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
private void sendNotification(String body, String title, String badge) {
Intent intent = new Intent(this, MainActivity.class);
intent.putExtra("from_notify", true);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, Integer.valueOf(badge),
intent,
PendingIntent.FLAG_ONE_SHOT);
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_stat_name)
.setContentTitle(title)
.setContentText(body)
.setPriority(Notification.PRIORITY_HIGH)
.setShowWhen(true)
.setVisibility(Notification.VISIBILITY_PUBLIC)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, notificationBuilder.build());
}
Receiver
Manifest
Ответы
Ответ 1
В последних версиях FCM (Firebase Cloud Messages) корпорация добра изменила порядок обработки уведомлений. Теперь оно (Андроид) само решает, как обработать пуш. Если приложение в бэкграунде, то пуш покажется с настройками по умолчанию. Цитата: When your app is in the background, Android directs notification messages to the system tray. A user tap on the notification opens the app launcher by default. Что значит: Когда ваше приложение в бэкграунде, Андроид сам покажет уведомление, при клике на который откроется ваше Апп. Подробности тут https://firebase.google.com/docs/cloud-messaging/android/receive. Это происходит для всех сообщений с типом Notification message Пример: { "to" : "bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...", "notification" : { "body" : "great match!", "title" : "Portugal vs. Denmark", "icon" : "myicon" } } Чтобы избежать этого, для самостоятельной обработки пушей используйте тип Data, как указано здесь https://firebase.google.com/docs/cloud-messaging/concept-options Пример { "to" : "bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...", "data" : { "Nick" : "Mario", "body" : "great match!", "Room" : "PortugalVSDenmark" }, } Соответственно, такая же проблема есть и наоборот - если все уведомления имеют тип Data, то они просто перестают показываться, потому что система ждёт Notification message.
Комментариев нет:
Отправить комментарий