In my Android app, I keep count of total events and update my notifications accordingly.
I keep track of the events (or message data) in a List
and the size of the List
is the running total for pending notifications. This List
is initialized in a class that extends Application
. So when the app starts (or notification is clicked on), it is reset to zero. A question arises:
W*hat if the User clears the Notification without opening the app.* The next Notification after that will show all List
data (Think gmail notifications: It will show the newest message AND the older ones still in the List
). So How/Where can I reset this variable when the user clears a notification?
FWIW, I am using Google Cloud Messaging
.
Here is how I handle my Notification now:
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());