Android:从通知栏删除通知
我已经创建了一个应用程序,并且通过一个事件我设法在 android 通知栏中添加通知。现在我需要示例如何从事件的通知栏中删除该通知?
I have created an application and with an event I manage to add notification in android notification bar. Now I need sample how to remove that notification from notification bar on an event ??
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(13)
您可以尝试这个快速代码
You can try this quick code
这很简单。您必须在NotificationManager上调用
cancel
或cancelAll
。 cancel方法的参数是要取消的通知的ID。请参阅 API:http://developer.android.com/参考/android/app/NotificationManager.html#cancel(int)
This is quite simple. You have to call
cancel
orcancelAll
on your NotificationManager. The parameter of the cancel method is the ID of the notification that should be canceled.See the API: http://developer.android.com/reference/android/app/NotificationManager.html#cancel(int)
您还可以在通知管理器上调用
cancelAll
,因此您甚至不必担心通知 ID。编辑:我被否决了,所以也许我应该指定这只会从您的应用程序中删除通知。
You can also call
cancelAll
on the notification manager, so you don't even have to worry about the notification ids.EDIT : I was downvoted so maybe I should specify that this will only remove the notification from your application.
这会有所帮助:
这应该会删除应用发出的所有通知
如果您通过调用创建通知
在服务内部。您可能需要调用
先取消通知。
this will help:
this should remove all notifications made by the app
and if you create a notification by calling
inside a Service.you may have to call
first,then cancel the notification.
只需像下面的代码一样设置 setAutoCancel(True) :
simply set setAutoCancel(True) like the following code:
如果您要从在前台启动的服务生成通知,
则
使用取消通知和发出通知不起作用。通知仍然出现在状态栏中。在这种特殊情况下,您将通过两种方式解决这些问题:
1>在服务内使用 stopForeground( false ):
2>通过调用活动销毁该服务类:
第二种方式更喜欢音乐播放器通知,因为这种方式不仅会删除通知,还会删除播放器......!
If you are generating Notification from a Service that is started in the foreground using
Then issuing
does't work canceling the Notification & notification still appears in the status bar. In this particular case, you will solve these by 2 ways:
1> Using stopForeground( false ) inside service:
2> Destroy that service class with calling activity:
Second way prefer in music player notification more because thay way not only notification remove but remove player also...!!
简短的一句话是:
或者取消所有通知是:
为 AndroidX 或支持库制作。
A short one Liner of this is:
Or to cancel all notifications is:
Made for AndroidX or Support Libraries.
请尝试这个,
Please try this,
它将帮助你
如果你像这样启动通知前台
那么你也应该停止前台服务
It will help you
If you start notification foreground like this
Then you should stop foregroundservice also
使用NotificationManager取消您的通知。您只需要提供您的通知ID
mNotificationManager.cancel(YOUR_NOTIFICATION_ID);
另请检查此链接
查看开发者链接
Use the NotificationManager to cancel your notification. You only need to provide your notification id
mNotificationManager.cancel(YOUR_NOTIFICATION_ID);
also check this link
See Developer Link
NotificationManager.cancel(id)
是正确的答案。不过,您可以通过删除整个通知渠道来删除 Android Oreo 及更高版本中的通知。这应该会删除已删除频道中的所有消息。以下是 Android 文档中的示例:
NotificationManager.cancel(id)
is the correct answer. Yet you can remove in Android Oreo and later notifications by deleting the whole notification channel. This should delete all messages in the deleted channel.Here is the example from the Android documentation:
在 Android API >=23 上,您可以执行类似的操作来删除一组通知。
On Android API >=23 you can do somehting like this to remove a group of notifications.
只需拨打ID:
Just call ID: