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

问题描述:

我正在使用推送通知和我希望每当新的推送通知来覆盖旧的推送通知,在我的情况下,这是很好,当我的应用程序是在前台,但当我把我的应用程序在后台,它正在通知面板中创建新的通知。如何用通知面板中的新推送通知覆盖新的推送通知

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

我想表明在通知面板 只有最新的通知,谢谢您提前

+0

只需使用相同的通知ID。它会用新的通知替换旧的通知。 –

+0

@SamuelRobert我总是使用相同的通知ID,但它正在创建新的通知,我不想要这个 – Sunil

+0

您是否在下面签出我的答案? –

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

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

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

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