2017-09-15 43 views
0

我正在使用推送通知和我希望每當新的推送通知來覆蓋舊的推送通知,在我的情況下,這是很好,當我的應用程序是在前臺,但當我把我的應用程序在後臺,它正在通知面板中創建新的通知。如何用通知面板中的新推送通知覆蓋新的推送通知

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

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

回答

2

當您從服務器配置通知時會發生這種情況。在這種情況下,系統不會調用onMessageReceived方法,而是使用系統托盤發送通知。

當您的應用在後臺時,Android會將通知消息導向系統托盤。用戶點擊通知會默認打開應用程序啓動器。

這包括包含通知和數據負載(以及通知控制檯發送的所有消息)的消息。在這些情況下,通知將傳遞到設備的系統托盤,並且數據有效載荷將按照啓動程序活動的目的附加內容進行傳遞。

爲了避免此問題,請將通知json對象從請求中移除到FCM服務器,並保留數據負載。即使應用程序處於後臺,這也會觸發onMessageReceived方法。您可以像現在一樣處理通知。