0

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());
TheLettuceMaster
  • 15,594
  • 48
  • 153
  • 259
  • How do you reset the counter to zero when the notification is clicked on? I have a counter set up as an int variable in SharedPreferences and I want to reset it to zero when the user clicks on the Notification dropdown shade. – AJW May 15 '18 at 03:19

1 Answers1

4

If you are working on API 11 or higher you can use setDeleteIntent(intent) method for builder. It will supply a PendingIntent to send when the notification is cleared explicitly by the user. So you can reset variable.

If you are working on lower API check this: How to check which notifications are active in status bar in Android Dev?

Community
  • 1
  • 1
Misagh Emamverdi
  • 3,654
  • 5
  • 33
  • 57
  • I am targeting API 16+ so this should work... added code above, fyi – TheLettuceMaster Sep 24 '14 at 20:11
  • marked correct; this worked perfect. Had to set up a Broadcast receiver – TheLettuceMaster Sep 24 '14 at 21:53
  • @Misagh Emamverdi How do you reset the counter to zero with setDeleteIntent(intent) when the notification is clicked on? I have a counter set up as an int variable in SharedPreferences and I want to reset it to zero when the user clicks on the Notification dropdown shade. – AJW May 15 '18 at 15:49
  • @AJW Just as you call `setContentIntent()` and passing a `PendingIntent`, call `setDeleteIntent()` passing a `PendingIntent` that fires a `BroascastReceiver`. In `onReceive()` method, reset your counter value in `SharedPreferences`. – Misagh Emamverdi May 16 '18 at 04:03
  • @Misagh Emamverdi Ok will try that. But all of the code I use is within JobIntentService. So do I set up the BroadcastReceiver as a static inner class within JobIntentService for the PendingIntent? – AJW May 16 '18 at 12:03
  • @AJW No you can directly create a `PendingIntent` that starts your service as I know. – Misagh Emamverdi May 19 '18 at 04:55