Android Notifications with Phone App Example


Sometimes you want your user to get notified of some event which has occurred. You can notify them with a notification. On clicking the notification user is presented with some other screen.

We can create notification using NotificationManager. It is simply a system service.

Android phone without notification

 

Android phone with notification

Create an instance of NotificationManager

 NotificationManager nm;

Now we need to create a method which can deal with the creation of notification.

	public void myNotification(){
		nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);    	
	}

As notification is a service and to use it we need to get its object from System Service. So you can use getSystemService() and assign the result to NotificationManager - nm.

Now we need to create an Intent which will help us to launch a new activity whenever user clicks on notification.

 Intent intent = new Intent(this, SecondActivity.class); 

But this intent is not enough, we need to create a pending intent to deliver the intent only when user clicks on notification.

 PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);	    

Now Notification.Compact helps to access features related to notification. Some of the features include :

  • Setting the icon for your notification.
  • Setting the title for your notification.
  • Setting the text to be displayed on notification.
NotificationCompat.Builder nb = new NotificationCompat.Builder(this).setSmallIcon(R.drawable.ic_launcher).setContentTitle("Notification from kb4dev").setContentText("android tutorials are available now");
	    nb.setContentIntent(pendingIntent);

Complete code to generate a notification

package com.example.kb4devnotification;

import android.os.Bundle;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.support.v4.app.NotificationCompat;
import android.view.Menu;

public class MainActivity extends Activity {
    NotificationManager nm;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		myNotification();
		
	}
	public void myNotification(){
		nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
	    Intent intent = new Intent(this, SecondActivity.class);
	    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
	    NotificationCompat.Builder nb = new NotificationCompat.Builder(this).setSmallIcon(R.drawable.ic_launcher).setContentTitle("Notification from kb4dev").setContentText("android tutorials are available now");
	    nb.setContentIntent(pendingIntent);
	    nm.notify(1, nb.build());
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}

Now you can create any activity named SecondActivity to get started whenever user clicks on notification.

We learned about creating notifications. We can also alert user with the help of Toast. You can use Toast if you want to display pop up in activity. But if your application is not having user focus then you can create notification to tell user that some event has happened. In the next tutorial we are going to add custom notification sound.