Status bar Notification Example in Android


Introduction to Notifications
              Notifications are used to alert users on some events that requires their attention. Notifications alert the users through various forms:
  • Display a status bar icon
  • Flash the LED
  • Vibrate
  • Ringtones
Status bar Notifications
               Status bar notifications indicates some ongoing background services such as downloading or playing music or displays an alert/information.To see the notification user needs to open the notification drawer. Notifications are handled by Notification Manger in Android. Notification Manager is a system service used to manage notifications.
               To create a status bar notification we need to use two classes, NotificationManager, Notification.
Lets see an simple example to display notification in status bar.
The NotificationManager is a system Service used to manage Notification. Get a reference to it using the getSystemService() method.

NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

Using the Notification Manager you can trigger new notifications, modify existing ones, or cancel that are no longer required. 

Notification should contain - icon, text, Time Stamp.
text - is the short description of the alert such as 'New message', 'New email'
Time Stamp-  Time stamp is used to sort the notifications, NotificationManager will sort the notifications.

Notification notification = new Notification(R.drawable.ic_launcher, "New Message", System.currentTimeMillis());

Next, lets create a pending intent which will be invoked when the message in status bar is clicked. Pending intents are intents and an action to do so that it can passed to some other applications which can execute this pendingintent.

Intent notificationIntent = new Intent(this, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,notificationIntent, 0);

I have passed the current activity to pendingIntent. The pendingINtent specified will be fired of a user clicks notification item.

The simplest approach is to use the setLatestEventInfo() method to specify the title and text fields.

notification.setLatestEventInfo(context, notificationTitle, notificationMessage, pendingIntent)

Finally post your notification to the status bar with a id. If a notification with same id already exists, it will get replaced with updated information.

notificationManager.notify(9999, notification);

That's it we are done !!

Notification Example

Check the Status bar for notification

Detailed View

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends Activity {
public void onCreate(Bundle savedInstanceState) {

Button notificationButton = (Button) findViewById(;

notificationButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Notify("Title: Meeting with Business",
"Msg:Pittsburg 10:00 AM EST ");

private void Notify(String notificationTitle, String notificationMessage) {
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Notification notification = new Notification(R.drawable.ic_launcher,
"New Message", System.currentTimeMillis());

Intent notificationIntent = new Intent(this, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,
notificationIntent, 0);

notification.setLatestEventInfo(MainActivity.this, notificationTitle,
notificationMessage, pendingIntent);
notificationManager.notify(9999, notification);


Source code of this application

More tutorials on Android Notifications coming soon !! Have one? Click here to submit and get paid !

Write comments
  1. hey thanks for this coding
    but i am getting error while i run the sample app...
    can you explain me what is 9999 in notify method ??

  2. thanks works

  3. thanks works


Popular Posts