Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to delete single fcm notification which comes when app is background?

I am developing an app where I am getting FCM notification, in that when the app is open onMessageRecieved() method triggers and I am notifying message based on tag and id and deleting notification based on id and tag. But when the app is in background onMesasageRecived() is not calling. How to attach id and tag notification or how to delete single notification based on some id when I am getting notification from the background.

see onMessageReceived

 @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
............
     notificationManager.notify("tag", notificationId, notification);
.......
}

for deleting the message

  private void clearNotifications() {

        NotificationManager nMgr = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

        nMgr.cancel("tag",notificationId);
    }

this is working but when the app is closed code is not working. can we notify or attach tag and id from the serverside payload

like image 487
android_jain Avatar asked Apr 25 '18 10:04

android_jain


1 Answers

If you check out the docs and here you'll see that if you have a notification payload your notification will be delivered to the system tray directly when your app is in the background, there's no way to intercept that. The same will happen if you have a notification payload with an option data payload, the notification will go straight to the tray and the data payload will be delivered to the intent of the launcher activity.

{
  "message":{
    "token":"bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...",
    "notification":{
      "title":"Portugal vs. Denmark",
      "body":"great match!"
    }
  }
}

As you're checking the id/tag in the onMessageReceive method, what you can do to guarantee that it will always call the onMessageReceived method is to remove the notification payload from your notification and add just a data payload. All data payloads are delivered to the onMessageReceived method.

  "message":{
    "token":"bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...",
    "data":{
      "Nick" : "Mario",
      "body" : "great match!",
      "Room" : "PortugalVSDenmark"
    }
  }
}
like image 152
Levi Moreira Avatar answered Oct 09 '22 06:10

Levi Moreira