Android: Displaying a status bar notification message

Post to Twitter

I recently was asked how to use Notifications for Android. The demo on the Android documentation page is a great start so I wrote a very quick application to demo how to use it.

[ad name=”Google Adsense”]

First, I created an Android project called “AndroidNotifications”.

The code is fairly simple so I’ll dump it here in it’s complete form:

package com.giantflyingsaucer;

import java.util.Timer;
import java.util.TimerTask;

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

public class AndroidNotifications extends Activity
	private final int NOTIFICATION_ID = 1010;

	public void onCreate(Bundle savedInstanceState)

		Button btn = (Button)findViewById(;

		btn.setOnClickListener(new View.OnClickListener()
			public void onClick(View v)
				Timer timer = new Timer();
				TimerTask timerTask = new TimerTask()
					public void run()
				timer.schedule(timerTask, 3000);

	private void triggerNotification()
		CharSequence title = "Hello";
		CharSequence message = "Hello, Android!";

		NotificationManager notificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
		Notification notification = new Notification(R.drawable.icon, "A New Message!", System.currentTimeMillis());

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

		notification.setLatestEventInfo(AndroidNotifications.this, title, message, pendingIntent);
		notificationManager.notify(NOTIFICATION_ID, notification);

The NOTIFICATION_ID allows you to grab the notification and remove it via your code if needed. Using a TimerTask I made it so that the notification shows up after 3 seconds from when the button is clicked. The real magic happens in the “triggerNotification()” method. The code is not complex and is pretty much the most minimal example I could come up with and keeping it pretty clear.

Here is the main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android=""
	        android:text="Create Notification"
	        android:padding="10dp" />

Keep in mind you can also make the LED lights, play a sound or use the vibration feature to also notify the user. Just make sure not to let your Android app get “annoying”. Better yet in your application let the user choose how they want to be notified via custom settings or default settings, etc.

Here is the final result:

[ad name=”Google Adsense – large sqr rounded”]

Post to Twitter

This entry was posted in Android, Open Source. Bookmark the permalink.

7 Responses to Android: Displaying a status bar notification message

  1. jcnet says:

    Thank you for posting this!

    By chance do you have the main.xml file for this project?

    I’m getting id cannot be resolved in

    Thanks again. good stuff.

  2. Chad Lung says:


    I’ve added the XML.


  3. John Lin says:

    May I ask one question ?
    How could I remove a notification?

  4. Chad Lung says:

    @John Lin,

    Typically a notification is removed by the user, not the system. On my phone I have to manually clear the notifications.


  5. Dhaval says:

    How can I show the Notification Bar at the right side of device vertically?

  6. Uzair says:

    Thanks you Dear 🙂 I finaly found solution from your blog

  7. Tuxen says:

    Thanks! 🙂

    To anyone wishing to remove the notify again simply do this:

    NotificationManager notificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);


Comments are closed.