Using Notification Bar – Android tutorial
In this tutorial we are going to learn how to use the Notification Bar: This is very useful, and is used almost in every application that runs in the background, or that is activates by events. In this example we will add a button that when pressed will open send a notification to the notification bar, with Title and Text, and will also make the Led of the phone blink with a purple color.
- Add notification Title
- Add notification Text
- Add Picture to the notification
- Make the Trackball Led Blink (For telephone that have the SW only)
- Chane the color of the blinking Led
<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/Button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Send Notification" />
</linearlayout>
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button1Action Button1Obj = new Button1Action();
Button1 = (Button) findViewById(R.id.Button1);
Button1.setOnClickListener(Button1Obj);
}/* End of onCreate Method */
We now add the class that takes care of the notification. To send a notification we will need to add an intent, with the text we want to add to the notification
Inside the UsingNotificationBar class we need to add the fallowing class:
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button1Action Button1Obj = new Button1Action();
Button1 = (Button) findViewById(R.id.Button1);
Button1.setOnClickListener(Button1Obj);
}/* End of onCreate Method */
public class Button1Action implements Button.OnClickListener
{
@Override
public void onClick(View arg0)
{
/* Get the notification manager */
mNotManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
/* Create a notification */
String MyText = "Text inside: By Firstdroid.com";
mNotification = new Notification(
R.drawable.icon, // An Icon to display
MyText, // the text to display in the ticker
System.currentTimeMillis() ); // the time for the notification
/* Starting an intent */
String MyNotifyTitle = "Firstdroid Rocks!!!";
String MyNotifiyText = "Firstdroid: our forum at www.firstdroid.com";
Intent MyIntent = new Intent( getApplicationContext(), UsingNotificationBar.class );
MyIntent.putExtra("extendedTitle", MyNotifyTitle);
MyIntent.putExtra("extendedText" , MyNotifiyText);
PendingIntent StartIntent = PendingIntent.getActivity( getApplicationContext(),
0,
MyIntent,
0);
/* Set notification message */
mNotification.setLatestEventInfo( getApplicationContext(),
MyNotifyTitle,
MyNotifiyText,
StartIntent);
mNotification.ledOnMS = 200; //Set led blink (Off in ms)
mNotification.ledOffMS = 200; //Set led blink (Off in ms)
mNotification.ledARGB = 0×9400d3; //Set led color
/* Sent Notification to notification bar */
mNotManager.notify( NotificationId , mNotification );
}/* End of method onClick */
}/* End of Class Button2Action_CDT */
Also, do not forget the imports we need:
{
@Override
public void onClick(View arg0)
{
/* Get the notification manager */
mNotManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
/* Create a notification */
String MyText = "Text inside: By Firstdroid.com";
mNotification = new Notification(
R.drawable.icon, // An Icon to display
MyText, // the text to display in the ticker
System.currentTimeMillis() ); // the time for the notification
/* Starting an intent */
String MyNotifyTitle = "Firstdroid Rocks!!!";
String MyNotifiyText = "Firstdroid: our forum at www.firstdroid.com";
Intent MyIntent = new Intent( getApplicationContext(), UsingNotificationBar.class );
MyIntent.putExtra("extendedTitle", MyNotifyTitle);
MyIntent.putExtra("extendedText" , MyNotifiyText);
PendingIntent StartIntent = PendingIntent.getActivity( getApplicationContext(),
0,
MyIntent,
0);
/* Set notification message */
mNotification.setLatestEventInfo( getApplicationContext(),
MyNotifyTitle,
MyNotifiyText,
StartIntent);
mNotification.ledOnMS = 200; //Set led blink (Off in ms)
mNotification.ledOffMS = 200; //Set led blink (Off in ms)
mNotification.ledARGB = 0×9400d3; //Set led color
/* Sent Notification to notification bar */
mNotManager.notify( NotificationId , mNotification );
}/* End of method onClick */
}/* End of Class Button2Action_CDT */
package firstdroid.tutorial.UsingNotificationBar;
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.widget.Button;
That is all, now run the code and you should see the fallowing:
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.widget.Button;


Any Question, Comment or Remark please refer to our forum: www.firstdroid.com forum
Enjoy,
Firstdroid.




I’m not creating a forum account just yet, but what’d be really useful here is either the full, working code files (say, a link to a tiny zip) or at least the full dump of them (instead of select snippets).
As-written there are several coding errors that make this uncompilable as it is (no type specified for mNotManager or mNotification – I sussed those out myself – and what’s NotificationId defined as and set to?)
Hi Marty,
After you told me it does not work, I tested it again, this tutorial works just fine. I started from Zero, and copied the snippets, and the code worked. Just copy to the right par of the code as explained.
But you are right about adding a zip file with the code.
I’ll do that in the next tutorial.
Thanks for the remark.
If you still have problem running this tutorial, send me a print of the error, I’ll try to help you
Adrian.
I am confirming what Marty said. The code as-is does not work. You didn’t declare mNotManager or mNotification. in the Button1Action class getSystemService is not defined, getApplicationContext is not defined (they are both methods in the Context class and this class does not extend context or any subclass of context). Honestly, I’d rather have no example than an example that doesn’t work.
Hi Marty,
One more thing, I am using Android 2.1 in my computer in this tutorial. I’ll check other android versions an update shortly.
Adrian.
Hey, cant we do notification like the Apple PNS in android. I want to see a dialog box appear as soon as we get a notification. Is it possible in Android, If yes how?
Hello all,martin NotificationId is an int number used as an id you specify it (just replace it with a number is you just want to test the program). George great tutorial, I have one question how to make the notification persistent, not go at the notification but at the upper part of the notification bar, like GPRSMonitor,Lookout,and ebuddy do?
thanks in advanced maxsap
Why do you declare a class within a class?
How can this ever work?
Hi David,
This is Called inner class in Java. It’s something it takes time to get used to if you come from a C or C++ environment.
There are several compelling reasons for using nested classes, among them:
– It is a way of logically grouping classes that are only used in one place.
– It increases encapsulation.
– Nested classes can lead to more readable and maintainable code.
George – The Droid.
hot to start activity on clicking the notification?
Hello George!
I’m new to Android and Java and this was a great example on how to show an app in the notification bar!
I have a question though; when I click the notification, my app (an mp3-player) restarts with new values (playing another song). I have implemented onRetainNonConfigurationInstance, and it works when I change the phone orientation and when I “start” a new instance of my app by clicking on the icon on the screen.
Any help would be appreciated.
/Roger
Hi! Thanks for this. Works fine
Hi i am using “notification.sound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM)” for getting notification sound and also i have set “notification.flags = notification.flags |Notification.FLAG_INSISTENT”. But still the sound i am getting is not continuous. I am using Android 2.2.
This tutorial does not work, you have to fix few missing things. This certainly does not help anyone who starts with Android and Java.
I fix the errors! Works fine now!
public void onClick(View arg0) {
/* Get the notification manager */
NotificationManager mNotManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
/* Create a notification */
String MyText = “Text inside: By Firstdroid.com”;
Notification mNotification = new Notification(
R.drawable.icon, // An Icon to display
MyText, // the text to display in the ticker
System.currentTimeMillis() ); // the time for the notification
/* Starting an intent */
String MyNotifyTitle = “Firstdroid Rocks!!!”;
String MyNotifiyText = “Firstdroid: our forum at http://www.firstdroid.com“;
Intent MyIntent = new Intent( getApplicationContext(), A1_Main.class );
MyIntent.putExtra(“extendedTitle”, MyNotifyTitle);
MyIntent.putExtra(“extendedText” , MyNotifiyText);
PendingIntent StartIntent = PendingIntent.getActivity( getApplicationContext(),
0,
MyIntent,
0);
/* Set notification message */
mNotification.setLatestEventInfo( getApplicationContext(),
MyNotifyTitle,
MyNotifiyText,
StartIntent);
mNotification.ledOnMS = 200; //Set led blink (Off in ms)
mNotification.ledOffMS = 200; //Set led blink (Off in ms)
mNotification.ledARGB = 0X9400d3; //Set led color
/* Sent Notification to notification bar */
mNotManager.notify( 1 , mNotification );
}
Hi all,
I am new to android development and I need a urgent help. My queries are :
1. Is there any way to keep figuring if a particular notification is there in the notification manager.
2. If there is any way can we launch it directly.
Please help me with this as its urgent.
Where is the code for clearing the notification.I am curious to know how you have implemented the code.