Status Bar Notifications

A status bar notification adds an icon to the system's status bar (with an optional ticker-text message) and an expanded message in the "Notifications" window.



After Notification generated, a selected notification icon and Ticket will be displayed on the status bar.

User can reveal the Notifications window by pulling down the status bar (or selecting Notifications from the Home options menu), to view the Title and Content.

Notification can be generated inside Service or Activity. This exercise show the basic steps to generate a Notification in Activity.

The most important class is NotificationManager and Notification.

In the exercise:
android.R.drawable.btn_star_big_on is a drawable icon in Android system resource, you can assign any drawable icon.
when is when the notification should be generated, System.currentTimeMillis() = NOW.
NOTIFICATION_ID is a number which is unique in your application.
contentIntent is the expected intent to handle the notification. It's the own activity in this exercise.

The generated Notification can be cleared by:
NotificationManager.cancel(NOTIFICATION_ID);

Modify main.xml to have two Buttons to generate and clear the Notification
<?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"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<Button
android:id="@+id/gen"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Generate Notification"
/>
<Button
android:id="@+id/clear"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Clear Notification"
/>
</LinearLayout>


AndroidNotification.java
package com.exercise.AndroidNotification;

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;

public class AndroidNotification extends Activity {

NotificationManager myNotificationManager;
private static final int NOTIFICATION_ID = 1;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

Button myGen = (Button)findViewById(R.id.gen);
myGen.setOnClickListener(myGenOnClickListener);
Button myClear = (Button)findViewById(R.id.clear);
myClear.setOnClickListener(myClearOnClickListener);


}

private void GeneratNotification(){

myNotificationManager =
(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);

CharSequence NotificationTicket = "*** Notification";
CharSequence NotificationTitle = "Attention Please!";
CharSequence NotificationContent = "- Notification is coming -";
long when = System.currentTimeMillis();

Notification notification =
new Notification(android.R.drawable.btn_star_big_on,
NotificationTicket, when);

Context context = getApplicationContext();

Intent notificationIntent = new Intent(this,
AndroidNotification.class);
PendingIntent contentIntent =
PendingIntent.getActivity(this, 0, notificationIntent, 0);

notification.setLatestEventInfo(context, NotificationTitle,
NotificationContent, contentIntent);

myNotificationManager.notify(NOTIFICATION_ID, notification);

}

Button.OnClickListener myGenOnClickListener =
new Button.OnClickListener(){

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
GeneratNotification();
}

};

Button.OnClickListener myClearOnClickListener =
new Button.OnClickListener(){

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
myNotificationManager.cancel(NOTIFICATION_ID);
}

};
}


Download the files.