【问题标题】:FCM Push Notifications are replacing when App is in Foreground当应用程序处于前台时,FCM 推送通知正在替换
【发布时间】:2021-07-11 20:32:27
【问题描述】:

有一些用于计划任务的 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();*/

            }

【问题讨论】:

  • 试试onesignal push,它很容易定制,ho head ace
  • 你能提供任何实现的链接吗@SamwinishereHere
  • 转到 onesignal.com
  • 你的 notificationData.getId() 总是唯一的吗?

标签: android firebase push-notification firebase-cloud-messaging


【解决方案1】:

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

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

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

用于替换通知中现有通知的标识符 抽屉。

如果未指定,每个请求都会创建一个新通知。

如果指定并且具有相同标签的通知已经在 如图所示,新通知将替换现有通知 通知抽屉。

【讨论】:

    【解决方案2】:

    "tag" 是 android 特定的选项 按以下格式指定:

    {
       notification: {
           title: "Title Here",
           body: "Body Here",
       },
       //android-specific options
       android: {
           notification: {
               tag: "My-Tag"
           }
       },
       token: tokenHere
    };
    

    接受的答案中给出的语法现在不起作用。它会抛出 Invalid JSON 错误并且不识别“tag”字段

    【讨论】:

      猜你喜欢
      • 2021-04-19
      • 2019-01-11
      • 2020-05-17
      • 2019-02-25
      • 2022-12-22
      • 1970-01-01
      • 2017-10-31
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多