Android - SPLessons

Android Notification

Chapter 21

SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

Android Notification

Android Notification

Android Notification explains you how to put a notification in the title bar of your application. This feature was added in API Level 1. It allows the user to trigger another action by selecting the notification. A user can also disable notifications for each application. This can be done via the Settings application of the Android device->Select Apps in the Settings->select the app you are interested in and uncheck the Show notifications checkbox to prevent the app showing notifications. In this lesson, we will explain about how to create and issue a notification:

Notifications in Android are represented by the Notification class. To create notifications, you must use the NotificationManager class, which can be received from the Context via the getSystemService() method.

NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

The Notification.Builder provides a builder interface to create a Notification object. A PendingIntent is used to specify the action that should be performed when the user selects the notification. The Notification.Builder allows you to add upto three buttons with definable actions to the notification.

Android 4.1 supports expandable notifications. Besides normal notification view, you can also define a big view, which will be displayed when a notification is expanded. There are three styles in the big view: big picture style, big text style, Inbox style. The following code explains you how to use the BigTextStyle():

Ex:

String big = "This is a big text";
Notification noti = new Notification.Builder(MainActivity.this)
.setContentTitle("New Notification")
.setContentText("Subject").setSmallIcon(R.drawable.ic_launcher)
.setContentIntent(pIntent)
.setStyle(new Notification.BigTextStyle().bigText(longText))
.addAction(R.drawable.ic_launcher, "Call", pIntent1)
.addAction(R.drawable.ic_launcher, "More", pIntent)
.build();

You can also call the cancel() method for a specific notification ID on the NotificationManager. When you call the cancelAll() method, all the notifications that were previously issued will be removed.

A pending intent is a token that you have given to a different application (e.g., notification manager, alarm manager or other 3rd party applications). The pending intent allows other application to access the permissions of your current application to execute a predefined set of code.

Ex:

Create MainActivity.java under src/<your packagename>.

MainActivity.java:

public class MainActivity extends Activity {
	  @Override
	  public void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	    setContentView(R.layout.activity_main);
	  }

	  @SuppressLint("NewApi") public void createNotification(View view) {
	    // Prepare intent which is triggered if the
	    // notification is selected
	    Intent intent = new Intent(this, NotificationReceiverActivity.class);
	    PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, 0);

	    Intent in=new Intent(Intent.ACTION_CALL);
	    in.setData(Uri.parse("tel:"+"7890675645"));
	    PendingIntent pIntent1 = PendingIntent.getActivity(this, 0, in, 0);

	    // Build notification
	    // when you click on call - a call will go to a number and when you click on more //another activity opens
	    Notification noti = new Notification.Builder(MainActivity.this)
	        .setContentTitle("New Notification")
	        .setContentText("Subject").setSmallIcon(R.drawable.ic_launcher)
	        .setContentIntent(pIntent)
	        .addAction(R.drawable.ic_launcher, "Call", pIntent1)
	        .addAction(R.drawable.ic_launcher, "More", pIntent)
	        .build();
	    NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
	    // hide the notification after its selected
	    noti.flags |= Notification.FLAG_AUTO_CANCEL;

	    notificationManager.notify(0, noti);

	  }
	}

Create activity_main.xml under res/layout folder.

activity_main.xml:




    <Button>
    </Button>


Create NotificationReceiverActivity.java under src/<your packagename>.

NotificationReceiverActivity.java :

public class NotificationReceiverActivity extends Activity {
	  @Override
	  protected void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	    setContentView(R.layout.notification);
	  	 }
	}

Create notification.xml under res/layout folder.

notification.xml:




    
    


AndroidManifest.xml:

You need to add call phone permission to dial a phone number.




    



    
        
            
                

                
            
        
        
    


The above code works for API level 16. For API below 16 use the following code.

NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
@SuppressWarnings("deprecation")
 Notification notification = new Notification(R.drawable.ic_launcher,"New Message",System.currentTimeMillis());
    
Intent notificationIntent = new Intent(this,NotificationReceiverActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,notificationIntent,0);    
notification.setLatestEventInfo(MainActivity.this, "Heading","Subject", pendingIntent);
notificationManager.notify(9999, notification);

Output: