Wednesday, February 15, 2012

Create status bar notifications in Android applications

Status bar notification is the most common and convenient way to alert a user of an application that a task has been completed (i.e. data download), that a message has been received or even to remind him a task that he needs to do in the near future etc. When using notifications the user is free to perform other tasks with his device and when a notification pops up he can return to the application that caused it, thus saving him plenty of time.

In this tutorial I will demonstrate how to create status bar notifications and interact with them. First of all, I am quoting the source code and then I am gonna get into details about it.
package com.androidsnippet.StatuBarNotification;

import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class StatusBarNotification extends Activity {

private Context mContext;
private Button mNotificationButton;
private NotificationManager mNotificationManager;
private int notificationCounter = 0;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

mContext = this;
mNotificationButton = (Button) findViewById(R.id.create_notification);
mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationButton.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Notification notification = new Notification(R.drawable.stat_notify_alarm, mContext.getString(R.string.new_notification), System.currentTimeMillis());
Intent intent = new Intent(mContext, NotificationActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent pendingIntent = PendingIntent.getActivity(mContext, notificationCounter,
intent, PendingIntent.FLAG_UPDATE_CURRENT);
notification.setLatestEventInfo(mContext, mContext.getString(R.string.notification_title),
mContext.getString(R.string.notification_message), pendingIntent);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notification.defaults |= Notification.DEFAULT_SOUND;
notification.defaults |= Notification.DEFAULT_VIBRATE;
mNotificationManager.notify(notificationCounter, notification);
notificationCounter++;
}
});
}
}
As you can see when the button is clicked a new notification is created at status bar.
  • At first,  construct a Notification object with the information needed to have a status bar, an icon, a piece of text that flows by in the status bar when the notification first activates and the time to show the notification in System.currentTimeMillis timebase
  • Next, construct an Intent object that is an abstract description of an operation to be performed. Here, it starts another Activity.
  • Assign this Intent to a PendingIntent object. By using, the PendingIntent the application does not perform the action described by the Intent immediately but when the user performs an action that activates the PendingIntent.
  • Set some flag fields. Here, the notification should be canceled when it is clicked by the user.
  • Set a sound and/or a vibration when the notification first activates. Here, I use the default sound and vibration pattern of the device but you can use any sound or vibration pattern you want. Do not forget to use VIBRATE permission in manifest file.
  • Finally, create the notification
Now if you click the button a notification is displayed in the status bar.
If you click the notification you will be navigated to the Activity described by Intent object.
So that's it! Find the whole project here! As always you are welcome to leave a comment! Cheers