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.


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.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
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;

	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		Button btn = (Button)findViewById(R.id.createNotificationButton);

		btn.setOnClickListener(new View.OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				Timer timer = new Timer();
				TimerTask timerTask = new TimerTask()
				{
					@Override
					public void run()
					{
						triggerNotification();
					}
				};
				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="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
	<Button
	        android:id="@+id/createNotificationButton"
	        android:layout_width="wrap_content"
	        android:layout_height="wrap_content"
	        android:text="Create Notification"
	        android:padding="10dp" />
</LinearLayout>

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:

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
    R.id.createNotificationButton

    Thanks again. good stuff.

  2. Chad Lung says:

    @jcnet,

    I’ve added the XML.

    Chad

  3. John Lin says:

    Thanks.
    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.

    Chad

  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);

    notificationManager.cancel(NOTIFICATION_ID);

Comments are closed.