quanyi
quanyi

Reputation: 55

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.

Upvotes: 5

Views: 4273

Answers (1)

Max Mumford
Max Mumford

Reputation: 2642

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.

Upvotes: 2

Related Questions