How can I clear firebase cloud message notifications?

I use firebase cloud message to send messages to my phone and I can successfully receive notifications. But in some cases, I do not click notifications to open my application, but manually open the application to go to foregroud. And I want, when I open the application, notifications in the notification panel should be automatically cleared.

+5
source share
1 answer

The following code will clear all notifications for your application, including those created using FCM. You can add this to the onResume method of your main activity:

NotificationManager manager = (NotificationManager) getApplicationContext() .getSystemService(Context.NOTIFICATION_SERVICE); manager.cancelAll(); 

Usually you cancel the notification by specifying the identifier that you gave it when it was created, however, since FCM creates the notification for you, you cannot know its identifier and therefore must cancel it by canceling all notifications as described above.

+1
source

Source: https://habr.com/ru/post/1261250/


All Articles