Posts Tagged Status Bar Notification with an example

Status Bar Notification with an Example

Notification as you know notifies the user of an event or events.A Status Bar Notification is simply the notification which appears on the Status bar of the system.

Now, lets look at how to launch notifications on the status bar.

For this we need two classes:

1)Notification and

2)Notification Manager

Firstly we need to retrieve a reference to NotificationManager(which is a system service in android that executes and manages all the notifications) using getSystemService() method.

This is how it is done:

NotificationManager nman;
nman=(NotificationManager) getSystemService(NOTIFICATION_SERVICE);

Next,we need to create an instance of the Notification class inorder to define the properties of the status bar notification – the icon that appears on the status bar when notification is launched, the notification text ,time of notification etc.

This is how I’ve done in my code:

Long when=System.currentTimeMillis();
Notification n;
n=new Notification(R.drawable.greenhome,"1",when);

Next, we need to write the code for what have to be done when the user clicks the specific notification.

Here is how its done:

Context context=getApplicationContext();
String Title1="Alert";
String Text1="Just an alert";
Intent notifintent=new Intent(context,Alerttest.class);
PendingIntent pending;
pending=PendingIntent.getActivity(context,0,notifintent,android.content.Intent.FLAG_ACTIVITY_NEW_TASK);
n.setLatestEventInfo(context, Title1, Text1, pending);

Now, to notify the user pass the notification object here its ‘n’ to NotificationManager:

nman.notify(0,n);

where 0(zero) is just the notification id to uniquely identify a notification.

To stop the notification:

nman.cancel(0);

which specifically cancels the notification with id=0

Or

nman.cancelAll();

which cancels all notifications.

Now that you have got a brief idea about launching a status bar notification, you can easily understand my example code below.

Here, I’m presenting an example in which I’ve used two activities namely MultiNotificationdemoActivity and Alerttest .

In the main activity MultiNotificationdemoActivity I’ve used two buttons ‘start’ and ‘stop’ where ‘start’-starts the notification(here I used two notifications one will lead you to the website of google when clicked and the other when attented will lead you to another activity(Alerttest)) and the ‘stop’ – stops the notifications.

In the second activity Alerttest , there is a TextView with text “WELCOME” and a button which when clicked returns to the first activity.

In my main activity which is MultiNotificationdemoActivity the code for launching notification is given. Just have a look at the code now.

public class MultiNotificationdemoActivity extends Activity {
/** Called when the activity is first created. */
Notification n;
NotificationManager nman;
Button start,stop;
Long when=System.currentTimeMillis();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
nman=(NotificationManager) getSystemService(NOTIFICATION_SERVICE);
start=(Button)findViewById(R.id.start);
stop=(Button)findViewById(R.id.stop);
start.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Context context=getApplicationContext();
String Title1="Alert";
String Text1="Just an alert";
String Title2="Browse";
String Text2="Google";
Intent notifintent=new Intent(context,Alerttest.class);
Intent ni=newIntent();
ni.setAction(android.content.Intent.ACTION_VIEW).setData(Uri.parse("http:/www.google.com"));         PendingIntent pending;
pending=PendingIntent.getActivity(context,0,notifintent,android.content.Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent p;
p=PendingIntent.getActivity(context,0,ni,android.content.Intent.FLAG_ACTIVITY_NEW_TASK);
n=new Notification(R.drawable.greenhome,"1",when);
n.flags=Notification.FLAG_AUTO_CANCEL;
n.setLatestEventInfo(context, Title1, Text1, pending);
nman.notify(0,n);
n=new Notification(R.drawable.icon_google,"2",when);
n.flags=Notification.FLAG_AUTO_CANCEL;
n.setLatestEventInfo(context, Title2, Text2, p);
nman.notify(1, n);
}
});
stop.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
nman.cancelAll();
}
});
}
}

Note: Here, I’ve used a flag FLAG_AUTO_CANCEL  inoder  to clear the notification after it is being attended.

Here is how it looks when you first run the Application:

When you click the Start button(just look at the two notifications):

After Dragging down the notification curtain:

After Clicking the Home icon (see how the home icon disappeared):

After Clicking the Google icon(see now the google icon too vanished from status bar):

, , , , , , , ,

9 Comments