首页 > 解决方案 > 当应用程序处于前台时,FCM 推送通知正在替换

问题描述

有一些用于计划任务的 Web 应用程序。如果添加了一项任务,通知将发送到应用程序,其中包含一些任务详细信息。如果添加了第二个任务,则第一个通知将替换为第一个通知。仅显示一个通知,即第二个通知。

当应用程序在后台时,会显示 2 个通知。

但是一个应用程序在前台只显示一个通知。请帮我。

任何帮助,将不胜感激。

这是我在应用程序处于前台时的通知代码。

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();*/

            }

标签: androidfirebasepush-notificationfirebase-cloud-messaging

解决方案


Android 使用通知 ID 来区分通知。因此,它无法区分通知并显示多个通知。

tag在通知负载中使用参数。

{
    "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.


推荐阅读