Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 app, but manually open the app to go into foregroud. And what I want is when I open the app, notifications in notification bar should be automatically cleared.

like image 943
quanyi Avatar asked Oct 29 '22 15:10

quanyi


1 Answers

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

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

Usually you would cancel a notification by specifying the ID you gave it when it was created, however as FCM is creating the notification for you, you can't know its ID, and so must cancel it by cancelling all notifications as above.

like image 125
Max Mumford Avatar answered Nov 16 '22 15:11

Max Mumford