如何计算Android中的通知数量和显示单个图标?

Tun*_*yen 10 eclipse android android-notifications android-notification-bar

我有多个Android通知,但是当我从我的Web服务器发送消息时,android设备会在状态栏上创建一个新的通知图标.我想计算未读通知的数量,statusbar用单个图标显示它,当读取通知时,通知必须更改未读通知计数.我该怎么做?在这张图片中看起来像"3 Others":通知图标

Mor*_*mer 8

请在此处查看答案:如果有多个通知,如何提供计数器

你只需要设置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);
Run Code Online (Sandbox Code Playgroud)

您必须通过该NotificationManager.notify方法发送通知,并始终使用相同的ID.正如文档所述,id是应用程序中该通知的唯一标识符.如果您重复使用相同的ID,它将只更新该通知的文本和编号.

要检查用户何时单击通知,您需要提供PendingIntent(请参阅教程).要检查用户何时清除通知,您需要使用仅在Api Level 11中可用的Notification.Builder.

  • 更新:现在您可以在Android支持包中使用`NotificationCompat`类.http://developer.android.com/intl/es/reference/android/support/v4/app/NotificationCompat.html (2认同)