如何算的通知数量,在Android中显示一个图标?图标、数量、通知、Android

2023-09-04 07:15:19 作者:北城以北、思念不归

我有多个Android的通知,但是当我发送邮件从我的网络服务器,在Android设备上创建状态栏新通知图标。我想算的未读通知的人数,单图标显示它状态栏,以及何时通知被读取后,通知有改变未读通知张数。我该怎么办呢?它看起来像其他3人在这个形象:通知图标

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

推荐答案

查看答案在这里:How给计数器如果超过一个通知是有

您只需要设置 Notification.number

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

您必须通过 NotificationManager.notify 方法发送您的通知,具有相同ID的所有时间。由于文件说,该ID是为你的应用程序中该通知的唯一标识符。如果重复使用相同的ID,它只会更新文本和编号的通知。

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.

要检查,当用户点击该通知,您需要提供一个PendingIntent(见的教程)。要检查当用户清除通知,您需要使用 Notification.Builder ,这只是在API层11可用。

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.