Wednesday, November 3, 2010

Using Statusbar Notification in android

This is an example of using Status Bar Notification in Android. Notifications are using in Android to notify the user of events. The classes Notification and NotificationManager is used create notification. NotificationManager is a system service. We get the instance using getSystemService. The following code snippet creates a default notification:

NotificationManager notificationManager = (NotificationManager)getSystemService
(Context.NOTIFICATION_SERVICE);

int icon = R.drawable.icon;
CharSequence text = "Notification Text";
CharSequence contentTitle = "Notification Title";
CharSequence contentText = "Sample notification text.";
long when = System.currentTimeMillis();

Intent intent = new Intent(this, NotificationViewer.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, intent, 0);

Notification notification = new Notification(icon,text,when);

notification.setLatestEventInfo(this, contentTitle, contentText, contentIntent);

notificationManager.notify(Constants.NOTIFICATION_ID, notification);
 
For creating a status bar notification, we create an instance of Notification class and uses notify method of NotificationManager to add the trigger the notification. Here the Notification class is created with three parameters, icon to display, notification text to ticker and the time at which the notification is displayed. Then we set the notification title, notification text and the Pending Intent to launch when the user clicks on the notification.
The notify method of the NotificationManager is used to trigger the notification. This will add the notification to the status bar area. When the use pulls down the status bar the notification title, text and icon will be shown.
Vibrating the phone
If we want to vibrate the phone when the notification is displayed, then we can set the vibration pattern or use the flag to use the default vibration. Following code snippet sets the default vibration.

notification.defaults |= Notification.DEFAULT_VIBRATE;
 
To vibrate we set the vibrate member of the notification class. This is an array of long values; first element is the millisecond to wait before start, second is the length of the first vibration, third is the next millisecond to stay off and forth is the next millisecond to stay on and so on. The pattern can be as long as we like. Following code snippet sets the vibration:

long[] vibrate = {0,100,200,300};
notification.vibrate = vibrate;
 
Flashing the LED If we want to flash the LED then we can use the defaults flag to DEFAULT_LIGHTS. Following code snippets adds the default LED flash. 

notification.defaults |= Notification.DEFAULT_LIGHTS;
 
To use our own color pattern, set the ledARGB, ledOffMS, and ledOnMS and add the FLAG_SHOW_LIGHTS to the flag field. Following code snippets add a custom color pattern.

notification.ledARGB = Color.RED;
notification.ledOffMS = 300;
notification.ledOnMS = 300; 
 
Using a custom view to display notification.
In the above example the notification is displayed in a default view. If we want to use our own custom view to display notification, then we can define the custom layout using the RemoteView. For this we create an instance of RemoteView and set the contentView field of the Notification class.
Note: If we are setting the contentView then we should set the contentIntent field also, otherwise an exception will occur.
The RemoteView provides helper methods to set the values of the layout items like setTextViewText, setProgressBar, setImageViewResource, etc…
The example provided here is a simple one to display default and custom view notifications. The custom view defines an image view, text view and a progress bar. Once the custom notification is triggered, the application creates a thread to start the progress bar update. This is a simple method display the progress indicator. Real world scenarios require more complicated method.