简体   繁体   中英

how to override new push notification with new push notification in notification panel

I am working with push notification and i want that whenever new push notification comes it overrides old push notification ,in my case this is going good when my app is in foreground but when i put my app in background it is creating new notification in notification panel.

MyFirebaseMessagingService.class

Intent intent = new Intent(this,TabActivityClass.class);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
            PendingIntent.FLAG_ONE_SHOT);
    Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
    {
        notificationBuilder.setSmallIcon(R.drawable.notification_logo)
                .setContentTitle(title)
                .setContentText(messageBody)
                .setAutoCancel(true)
                .setSound(defaultSoundUri)
                .setContentIntent(pendingIntent);
    } else
    {
        notificationBuilder.setSmallIcon(R.drawable.notification_logo)
                .setContentTitle(title)
                .setContentText(messageBody)
                .setAutoCancel(true)
                .setSound(defaultSoundUri)
                .setContentIntent(pendingIntent);
    }
    NotificationManager notificationManager =
            (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());

Screenshot 在此处输入图片说明

I want to show only latest notification in notification panel Thank you in advance

This happens when you configure notification from server. In that case system won't invoke the onMessageReceived method, rather uses a system tray to send out a notification.

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.

This includes messages that contain both notification and data payload (and all messages sent from the Notifications console). In these cases, the notification is delivered to the device's system tray, and the data payload is delivered in the extras of the intent of your launcher Activity.

In order to avoid this issue remove the notification json object from the request to FCM server and just keep the data payload. This will trigger the onMessageReceived method even if the app is in background. You can handle the notification there just like you are doing now.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM