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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With