在Android中如何计数通知数和显示单个图标? [英] How to count number of notification and display single icon in Android?

查看:148
本文介绍了在Android中如何计数通知数和显示单个图标?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有多个Android通知,但是当我从网络服务器发送消息时,Android设备会在状态栏上创建一个新的通知图标。我想计算未读通知的数量,在状态栏上显示单个图标,当通知被读取时,通知必须更改未读通知计数。我该怎么做?在此图像中看起来像3其他:通知图标

解决方案

在这里查看答案:如果有多个通知在那里,如何给计数器



你只需要设置 Notification.number

 通知通知=新通知(R.drawable.direction,Cool Notification,
系统。的currentTimeMillis());
/ ********喜欢这个********* /
notification.number = notificationCount ++;
/ ********喜欢这个********* /

notification.setLatestEventInfo(上下文,Cool Notification Title,
更新通知信息,null);


NotificationManager nm =(NotificationManager)context.getSystemService(context.NOTIFICATION_SERVICE);
nm.notify(R.id.my_motification,notification);

您必须通过方法,一直使用相同的id。如文档所述,id是您应用程序中该通知的唯一标识符。如果您重复使用相同的ID,它将只更新该通知的文本和数字。



要检查用户点击通知的时间,您需要提供一个PendingIntent (请参阅教程)。要查看用户何时清除通知,您需要使用通知。只能在Api级别11中使用。


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

解决方案

Check out the answer here: How to give counter if more than one Notifications are there

You just have to set Notification.number:

Notification notification = new Notification(R.drawable.direction, "Cool Notification",
                System.currentTimeMillis());
        /********LIKE THIS*********/
        notification.number = notificationCount++;
        /********LIKE THIS*********/

        notification.setLatestEventInfo(context, "Cool Notification Title",
                "updated notificaiton message", null);


        NotificationManager nm = (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE);
        nm.notify(R.id.my_motification, notification);

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.

这篇关于在Android中如何计数通知数和显示单个图标?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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