Custom Notification in Android
Custom Notification
With Android 3.0 “Honeycomb” new notification have have become customizable. This allows to display rich content within the notification panel. While this is of course one of the features to be used with care — not to overload the GUI — it can be quite useful.
There are two two options open:
-
Designing a
android.widget.RemoteViews
to display the notification data. This is done by callingandroid.app.Notification.Builder.setContent
. (available since Android 3.0). -
Use a notification style (
android.app.Notification.Style
) to create a view which can switch between a standard views and expanded view (available since Android 4.2).
After evaluating them both — including some try error and abandon what does not work at all my suggestions are:
-
If your data fits in any of the new styles use them. Preferably using
android.support.v4.app.NotificationCompat.Builder
so backward compatibility is ensured. -
If it does not fit into one of the new styles then don’t try create a custom style by sub-classing
android.app.Notification.Style
. To much of the class, its factory companionandroid.app.Notification.Builder
as well as the associated resources are declared private. So you eventually end up reimplementing the whole notification styles framework. -
If you wish to create a true custom notification then create an
android.widget.RemoteViews
.
In rest of the Article I show how you create a custom notification which can display two lines of text — instead of just one line like it is possible with the normal notification.