简体   繁体   English

Android通知驳回了回调?

[英]Android notification dismissed callback?

my app searches for new articles and sends a notification like "5 new articles". 我的应用程序搜索新文章并发送“5篇新文章”之类的通知。 However when i send another one, i want to have it update the text to lets say there were 3 new so something like "8 new articles" BUT "3 new articles" if the user has dismissed that previous notification. 然而,当我发送另一个,我想让它更新文本,让我们说有3个新的所以像“8篇新文章”但“3篇新文章”如果用户已经驳回了之前的通知。 I hope you get it. 我希望你明白。

Is there a way to know that notification was dismissed so i can reset the count? 有没有办法知道通知被驳回,所以我可以重置计数?

Thanks ! 谢谢 !

This is a rather belated answer but I was looking to find out how to do this myself so perhaps it'll be useful to others. 这是一个相当迟来的答案,但我想找到自己如何做到这一点,所以也许它对其他人有用。 In API level 18 the following service was introduced which should make this straightfoward as you can now get a callback whenever a notification is added or removed: 在API级别18中引入了以下服务,这应该是直接的,因为您可以在添加或删除通知时获得回调:

https://developer.android.com/reference/android/service/notification/NotificationListenerService.html https://developer.android.com/reference/android/service/notification/NotificationListenerService.html

In particular for the original question see the onNotificationRemoved method 特别是对于原始问题,请参阅onNotificationRemoved方法

The other option mentioned by jpop above is to use the builder's setDeleteIntent method when creating the notification, which will give a callback when the notification is deleted. 上面jpop提到的另一个选项是在创建通知时使用构建器的setDeleteIntent方法,这将在删除通知时进行回调。 This would then require you to maintain the state of the existing raised notifications somewhere else as it only tells you when something is added, not removed. 这将要求您在其他地方维护现有引发通知的状态,因为它只会告诉您何时添加某些内容,而不是删除。

If your app has a database you can do this: Create a table that has some fields like id as int, article name as string, and isdismissed as boolean. 如果您的应用程序有数据库,您可以这样做:创建一个表,其中包含一些字段,如id为int,文章名称为字符串,isdismissed为boolean。 So, each time you want to send a notification, you should count the records that the isdismissed field equals false. 因此,每次要发送通知时,都应计算isdismissed字段等于false的记录。 In the other hand, each time user select a notification, the related isdismissed field must be equal true. 另一方面,每次用户选择通知时,相关的isdismissed字段必须等于true。

In addition, this sample from developer.android.com maybe can help you: 此外,developer.android.com的这个示例可能可以帮助您:

http://developer.android.com/guide/topics/ui/notifiers/notifications.html http://developer.android.com/guide/topics/ui/notifiers/notifications.html

the listener don't work since android Nougat, a new access rule have been added 自从Android Nougat添加了新的访问规则后,监听器无法正常工作

on android device : parameters -> applications -> gear menu -> spécial access -> notifications access 在Android设备上:参数 - >应用程序 - >齿轮菜单 - >spécial访问 - >通知访问

use : catch on swipe to dismiss event 使用: 抓住滑动以解除事件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM