如何清除Firebase云消息通知? [英] How can I clear firebase cloud message notifications?

查看:125
本文介绍了如何清除Firebase云消息通知?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Firebase云消息将消息发送到手机,并且可以成功接收通知.但是在某些情况下,我不会单击通知来打开我的应用程序,而是手动打开该应用程序以放弃.我想要的是打开应用程序时,通知栏中的通知应自动清除.

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.

推荐答案

以下代码将清除您应用的所有通知,包括由FCM创建的通知.您可以将其添加到您的主要活动的onResume方法中:

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();

通常,您可以通过指定创建时给定的ID来取消通知,但是当FCM为您创建通知时,您不知道其ID,因此必须通过取消所有通知来取消该通知,如上所述

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.

这篇关于如何清除Firebase云消息通知?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆