One of most useful features on Android are the Notifications. While iOS just got them – Android keeps improving them. JellyBean has presented some sweet notifications’ features. Larger area, multiline text entry, list of different items and pictures. There’s also an option to create your own custom layout to present there (though I think it potentially can ruin the uniformity of the notifications design). All those “tall” notifications can be also collapsed/expended to save some space. One more thing, and this could be the coolest of all, is the option to add **actions **to the notifications.
(Sample notifications on JellyBean. Taken using NotificationCompat2 sample app, by Jake Wharton)
Three actions don’t (always) make it right.
Each notification can suggest up to 3 actions, but what if I want more? Maybe I want that a click on one action will give some other actions to pick from? I know it’s not a common case, but I can think of several cases where it can be useful. Since all the notifications are built on the same way AppWidgets are built, using RemoteViews, there are not much place for run-time manipulation.
Seems like the old switch-a-roo will do the trick here.
Changing the actions is possible by replacing the whole notification with a new one. The replacing notification can have a different title and different actions from the previous one, but it must have the same notification id. This is how it’ll look like:
How to do that?
First, let’s clarify something – notification’s actions don’t just have an OnClickListener like regular buttons, they fire an Intent using a PendingIntent. A receiver needs to receive that Intent and “notify” the replacing notification to the Android’s Notification Service. The receiver can be a BroadcastReceiver, Service or even an Activity, but we want something that doesn’t take much resources/loading-time (as an Activity) or doesn’t have the potential to be called from other applications (as a BroadcastReveiver). A Service, or even simpler – IntentService, will fit perfectly here.
This service supports the “SWITCH_NOTIFICATION” action, which has 2 extras:
Notification Id: Must be the same as the notification we’re replacing
Notification object: The second notification we replace the current notification with
Luckily for us – the Notification class is Parcelable, meaning I can pass it as one of the Intent’s extras with no problem.
To illustrate this idea, I used Jake Wharton’s NotificationCompat2 project, which gives a simple API to build notifications for all Android versions from 1.6 to 4.1. My service was added to the manifest
I created the first notification, the one to be replaced:
The intentReplaceNotification _is the PendingIntent fired when pressing the “More..” action on the notification. This pending intent is the one in charge of calling the _NotificationReplaceService with the replacing notification.
Here we created the replacing notification, and passing it to the replacing action intent. This intent will start my _NotificationReplaceService, _which will use the intent’s extras to replace the main notification.
All that is left now to do is to notify the main notification to the Android’s Notification Service
And that’s it!
You can check the full code on my GitHub account. Just please do me one favor – don’t abuse this method to create a complex menu in the users’ notification bar, think about your users!