简体   繁体   中英

FCM Push Notifications are replacing when App is in Foreground

There is some web application for planning tasks. If one task is added, Notification will send to the app with some task details. If the second task is added, First notification is replaced with the first notification. only one notification is displaying that is the second one.

when an app is in the background, 2 notifications are showing.

But an app is in the foreground only one notification is displaying. Please help me.

Any help would be appreciated.

This is my code for notifications when an app is in the foreground.

if (!NotificationUtils.isAppIsInBackground(getApplicationContext())){
            Intent intent = new Intent(Config.PUSH_NOTIFICATION);
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK );
            intent.putExtra("Plan","fromFCM");
            broadcastManager.sendBroadcast(intent);
            Bitmap bitmapIcon = BitmapFactory.decodeResource(getResources(), R.drawable.notify_app_icon);
            PendingIntent pendingIntent = PendingIntent.getActivity(this,0,intent,PendingIntent.FLAG_ONE_SHOT);
            NotificationCompat.Builder notificationBulder = new NotificationCompat.Builder(this);
            notificationBulder.setContentTitle(notificationData.getTitle());
            notificationBulder.setContentText(notificationData.getTextMessage());
            notificationBulder.setSmallIcon(R.drawable.notify_app_icon);
            notificationBulder.setLargeIcon(bitmapIcon);
            notificationBulder.setStyle(new NotificationCompat.BigTextStyle().bigText(notificationData.getTextMessage()));
            notificationBulder.setAutoCancel(true);
            //notificationBulder.setSmallIcon(R.mipmap.ic_launcher);
            notificationBulder.setContentIntent(pendingIntent);
            NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
            if (notificationManager != null) {
                notificationManager.notify(notificationData.getId(), notificationBulder.build());
           /* Plan_Details plan_details = new Plan_Details();
            plan_details.refreshPlan();*/

            }

Android uses notification id to differentiate between the notifications. Therefore, it can not differentiate between the notifications and shows multiple notifications.

Use tag parameter in the notification payload.

{
    "notification" : {
        "title" : "Notification Title",
        "body" : "Notification Body",
        "tag" : "your_unique_tag"
    }
}

Identifier used to replace existing notifications in the notification drawer.

If not specified, each request creates a new notification.

If specified and a notification with the same tag is already being shown, the new notification replaces the existing one in the notification drawer.

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