Android Notification Manager Example

person folder_openAndroidaccess_time April 27, 2015

In this tutorial we are going to learn how to use NotificationManager to display status bar notifications. Notifications are typically to used by the application to notify the user about incoming messages, application updates, ongoing background tasks etc.,.

See flowing Steps :

  1. Create MainActivity.class and NotificationActivity.class.
  2. Use  NotificationCompat.Builder object to create the user interface for the notification.
  3. Create a PendingIntent to specify the action to be done when the user clicks on the status bar notification.

1. Manifest file

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.stackandroid.notification"
    android:versionCode="1"
    android:versionName="1.0" >
    <uses-sdk
        android:minSdkVersion="14"
        android:targetSdkVersion="21" />
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name="NotificationActivity"
            android:label="@string/app_name"
            android:parentActivityName="com.stackandroid.notification.MainActivity" >
        </activity>
    </application>
</manifest>

2. Activity layout file

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.stackandroid.notification.MainActivity" >
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="My First Screen"
        android:textColor="#0404B4" />
    <Button
        android:id="@+id/btnShow"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="showNotification"
        android:text="Notify Me" />
</LinearLayout>
 

3. Main Activity code

package com.stackandroid.notification;
import android.app.Activity;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;
import android.view.View;
public class MainActivity extends Activity {
	public static final int notificationID=2000;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
	}
	public void showNotification(View view)
	{
		// step -1  Create notification builder
		NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this);
		// step -2  Setting notification properties
		mBuilder.setSmallIcon(R.drawable.ic_launcher);
		mBuilder.setContentTitle("Notification Alert, Click Me!");
		mBuilder.setContentText("Hi, Welcome to http://www.stackandroid.com");
		// step -3  Attach details
		Intent resultIntent = new Intent(this, NotificationActivity.class);
		// Adds the Intent that starts the Activity to the top of the stack
		PendingIntent pendingIntent = PendingIntent.getActivity(
				this,
				0,
				resultIntent,
				Intent.FLAG_ACTIVITY_NEW_TASK);
		mBuilder.setContentIntent(pendingIntent);
		// step -4  Issue the notification
		NotificationManager mNotificationManager =
				(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
		// notificationID allows you to update the notification later on.
		mNotificationManager.notify(notificationID, mBuilder.build());
	}
}

The following activity will be displayed when the user clicks on the status bar notification.

package com.stackandroid.notification;
import android.app.Activity;
import android.app.NotificationManager;
import android.graphics.Color;
import android.os.Bundle;
import android.widget.TextView;
public class NotificationActivity extends Activity {
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// Dynamically create TextView
		TextView txt=new TextView(this);
		// set text on the textView
		txt.setText("This is our Notification detail Screen");
		txt.setTextColor(Color.BLUE);
		txt.setTextSize(24);
		setContentView(txt);
		NotificationManager n = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
		n.cancel(MainActivity.notificationID);
	}
}

4. Demonstra

notificationmanager first screen
notificationmanager first screen

 

If you click the button , You will receive the notification alert. clickable of an alert, take you to second detailed screen.

notificationManager alert detailed screen
notificationManager alert detailed screen

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">