Android 中如何统计通知数量并显示单个图标?
我有多个 Android 通知,但是当我从网络服务器发送消息时,Android 设备会在状态栏上创建一个新的通知图标。我想计算未读通知的数量,用单个图标将其显示在 statusbar
上,并且当阅读通知时,通知必须更改未读通知计数的数量。我该怎么做呢?在此图片中,它看起来像“3 Others”:通知图标
I have multiple Android notification, but when I send a message from my web server, the android device creates a new notification icon on status bar. I want to count the number of unread notification, display it on statusbar
with single icon, and when a notification is read, the notification has to change the number of unread notification count. How can I do it? It's look like "3 Others" in this image: Notification Icon
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在此处查看答案:如何提供计数器如果有多个通知,
您只需设置
Notification.number
:您必须通过
NotificationManager.notify
方法发送通知,并且始终使用相同的 id。正如文档所述,id 是应用程序中该通知的唯一标识符。如果您重复使用相同的 ID,它只会更新该通知的文本和编号。要检查用户何时单击通知,您需要提供 PendingIntent(请参阅 教程)。要检查用户何时清除通知,您需要使用 通知。仅在 Api 级别 11 中可用的构建器。
Check out the answer here: How to give counter if more than one Notifications are there
You just have to set
Notification.number
:You have to send your notification through the
NotificationManager.notify
method, with the same id all the time. As the documentation says, the id is a unique identifier for that notification within your application. If you reuse the same id, it will just update the text and number for that notification.To check when the user clicks on the notification, you need to provide a PendingIntent (see the tutorial). To check when the user clears the notifications, you need to use the Notification.Builder that is only available in the Api Level 11.
好吧,这个代码对我有用:
按照这个链接 获取完整教程。
我认为
n.number=++count;
是负责对通知进行计数的人Well this codes work for me:
Follow this link for complete tutorial.
I think the
n.number=++count;
is the one responsible for counting the notification您将需要为每个数字使用不同的位图,并设置图标以显示与剩余通知数量相对应的位图。
要跟踪通知,您只需在 SharedPreferences 中设置一个计数器,为每个新通知添加该计数器,并在读取通知时减一(如果一次显示所有通知则减为零)。
You will need a different bitmap for each number, and set the icon to show the bitmap that corresponds to the number of notifications left.
To keep track of the notifications, you can simply set a counter in SharedPreferences, add to it with each new notification, and reduce by one when a notification is read (or to zero if you show all the notifications at once).