处理通知总数

时间:2014-09-24 19:44:48

标签: android android-notifications google-cloud-messaging

在我的Android应用中,我会记录总事件并相应地更新我的通知。

我会跟踪List中的事件(或消息数据),List的大小是待处理通知的运行总计。此Listextends Application的类中初始化。因此,当应用程序启动(或点击通知)时,它将重置为零。出现了一个问题:

如果用户在未打开应用程序的情况下清除通知,则会显示*。*之后的下一个通知将显示所有List数据(Think gmail通知:它将显示最新消息而较旧的仍然在List)。 那么当用户清除通知时,如何/在哪里可以重置此变量?

FWIW,我正在使用Google Cloud Messaging

以下是我现在处理通知的方式:

            NotificationCompat.Builder mBuilder =
                    new NotificationCompat.Builder(this)
                            .setSmallIcon(R.drawable.ic_launcher)
                            .setContentTitle("My App")
                            .setStyle(new NotificationCompat.BigTextStyle()
                                    .bigText(msg))
                            .setLights(0xFF0000, 500, 500)
                            .setNumber(MyApp.events.size())
                            .setContentText(msg);

            NotificationCompat.InboxStyle inboxStyle =
                    new NotificationCompat.InboxStyle();

            // Sets a title for the Inbox style big view
            inboxStyle.setBigContentTitle("MyApp");
            for (int i=0; i < MyApp.events.size(); i++) {
                inboxStyle.addLine(MyApp.events.get(i));
            }
            if (MyApp.events.size() >= 8) {
                inboxStyle.setSummaryText("+" + (MyApp.events.size() - 7) + " more events.....");
            } else {
                inboxStyle.setSummaryText("Recent Events");
            }
            mBuilder.setStyle(inboxStyle);
            mBuilder.setNumber(MyndQuest.events.size());

            mBuilder.setAutoCancel(true);
            mBuilder.setContentIntent(contentIntent);
            mNotificationManager.notify(NID, mBuilder.build());

1 个答案:

答案 0 :(得分:4)

如果您使用的是API 11或更高版本,则可以使用setDeleteIntent(intent)方法进行构建。当用户明确清除通知时,它将提供PendingIntent发送。所以你可以重置变量。

如果您正在使用较低的API,请检查:How to check which notifications are active in status bar in Android Dev?