I just had the same issue and I also looked at Gmail for the appropriate behaviour. I first studied the following scenarios:
Assuming a user is actively using the Gmail app (disregard any interaction with the browser version), and then navigates away from it:
- From that point on, if the user receives a new email, it will get a notification.
- If the notification is still there and the user receives a new email, the notification will be updated to a summary containing the title of the N most recent emails since the user last used the app.
- If the user dismisses the notification and another email arrives, another summary notification is issued.
- If the user navigates back into the app (either by clicking the notification or not), any existing notifications are dismissed.
In API 18, Android added support to retrieve active notifications using NotificationListenerService, however that's too recent for the app I'm working on (min API 14), not to mention you can't rely on that to get the existing notification information if it has already been dismissed by the user.
So the solution I found was to persist information about the notifications already issued locally, use that information to create either a single notification or a summary, and clear everything when the user opens the app again.
To persist the notification information, I created a simple model similar to the following:
public class NotificationBundle {
private String mText;
// Add any other relevant information about your notification here,
// particularly what you used to create your notification intent
// i.e. an item/message id to highlight, maybe?
public String getText() {
return mText;
}
public void setText(final String text) {
mText = text;
}
}
The idea is to create an instance of those for each notification you issue.
Then you have to have in place a way to persist your list of NotificationBundle
objects. I used SharedPreferences
to do that, but you could use whatever else suits you better (like a DB table). To persist a List<NotificationBundle>
in SharedPreferences
I used Gson
to serialize the array into json, and then saved that as a string. Assuming you can use SharedPreferences
, you can find how to do the serialization in this answer.
With that structure in place, basically what's left for you to do is:
When you have to issue a notification:
- Create a new
NotificationBundle
with the information you want to notify.
- Retrieve your existing
List<NotificationBundle>
from SharedPreferences
- If your list of bundles is empty, you will issue a single notification. If it's not, you will issue a summary - in this case you can use your list of bundles to construct the content of your summary. A good article on summary notifications is [Using Big View Styles].
- Add your new
NotificationBundle
(from 1) into your existing List<NotificationBundle>
(from 2) and save it to SharedPreferences
.
- Issue your notification using
NotificationManager.notify()
. If you always use the same notification id here, it will create a new notification if none from your app are currently visible, or it will just update it if a previous notification is visible.
On your onResume()
method of your main Activity, make sure to dismiss all notifications with NotificationManager.cancelAll()
. Also make sure to remove from your SharedPreferences
your existing List<NotificationBundle>
.
And that should do the trick.