Using NotificationManager in ANDROID for showing notification, sample code.

By | March 18, 2011

Multiple Notifications are a unique feature in ANDROID phones. This tutorial will show you how to create notification from your application.
ANDROID uses the NotificationManager class to create the notification.
Let’s look at the code.

package com.Webviews;

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.view.View.OnClickListener;
import android.widget.Button;

public class NotificationActivity  extends Activity implements View.OnClickListener {

private Button button, clear;
private NotificationManager mManager;
private static final int APP_ID = 0;

@Override

public void onCreate(Bundle savedInstanceState) {

  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);
  button = (Button) this.findViewById( R.id.my_button);
  clear = (Button) this.findViewById( R.id.cancel);
  clear.setOnClickListener(new OnClickListener() {

	@Override
	public void onClick(View arg0) {

		mManager.cancel(APP_ID);
	}
  });
  button.setOnClickListener(this);
  mManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
}
@Override
public void onClick(View v) {

  Intent intent = new Intent(this,NotificationActivity.class);
  Notification notification = new Notification(R.drawable.icon,"Notification!!", System.currentTimeMillis());
  notification.setLatestEventInfo(NotificationActivity.this,"Send by which application","Description of the notification",
		  					      PendingIntent.getActivity(this.getBaseContext(), 0, intent,
		  					      PendingIntent.FLAG_NO_CREATE));
  mManager.notify(APP_ID, notification);
}
}

Explanation.

mManager.notify(APP_ID, notification);

This line in the code sends the notification where notification is the object of the Notification class where you can set the data and title for the notification.

This line will cancel the notification created by this application.

mManager.cancel(APP_ID);

The layout file for the above code “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"
    >
<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/hello"
    />

<Button
	android:text="Send Notification"
	android:id="@+id/my_button"
	android:layout_width="wrap_content"
	android:layout_height="wrap_content">
</Button>

<Button
	android:text="Cancel Notification"
	android:id="@+id/cancel"
	android:layout_width="wrap_content"
	android:layout_height="wrap_content">
</Button>

</LinearLayout>

Please leave your valuable comments on this post.

5 thoughts on “Using NotificationManager in ANDROID for showing notification, sample code.

  1. Pingback: Using NotificationManager in ANDROID for showing notification, sample code. | Android Blog

  2. Pingback: Send Data when Clicking on a Notification in Android? | Coderz Heaven

  3. niza

    hai..
    is there any tutorial on how receiving notification from mysql db..

    thanks in advance..

    Reply
    1. James

      You can write your own. Create a service that runs in background and check your php script at intervals and if there is something show a notification.

      Reply

Leave a Reply

Your email address will not be published. Required fields are marked *