如何使用通知面板中的新推送通知覆盖新的推送通知

时间:2017-09-15 05:30:00

标签: android push-notification

我正在使用推送通知和我希望每当新的推送通知出现时它会覆盖旧的推送通知,在我的情况下,当我的应用程序在前台但是当我把我的应用程序在后台正在通知面板中创建新通知。

  

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

截图   enter image description here

我想在通知面板中仅显示最新通知 提前谢谢

1 个答案:

答案 0 :(得分:2)

从服务器配置通知时会发生这种情况。在这种情况下,系统不会调用onMessageReceived方法,而是使用系统托盘发送通知。

  

当您的应用在后台时,Android会将通知消息定向到系统托盘。用户点按通知会默认打开应用启动器。

     

这包括包含通知和数据有效负载的消息(以及从Notifications控制台发送的所有消息)。在这些情况下,通知将传递到设备的系统托盘,并且数据有效负载将在启动器活动的附加内容中传递。

为了避免此问题,请将通知json对象从请求中删除到FCM服务器,并保留数据有效负载。即使应用程序处于后台,这也会触发onMessageReceived方法。您可以像现在一样处理通知。

相关问题