• Have Any Question?
  • contact@eitworld.com

Taking Action With Android Notification

    • If we want to perform actions on the android notification then we can perform, it is optional part.
    • We can display the normal or simple text or we can display the entire message on the click of notification or we can show another message when we click on the notification.
    • By using the notification, we can perform the various kinds of actions such as we can add the text field or button to the notification.
    • When we click on the notification then application opens, it is common option in android.
    • To open the application on notification click, we have to use the setContentIntet() method and pass the object of the PendingIndent class.
    • When we create the PendingIndent class object then we have to pass the object of the Intent class. This intent is used to define the action to open the particular activity.
    • Following are the syntax:
Intent yrIntent = new Intent(this,yrActivity.class); pendingIntent pi = new pendingIntent(yrIntent); ncb.setContentIntent(pi);

The following example shows the notification in android. The xml layout file is as follows:

<RelativeLayout xmlns:android=”http://schemas.android.com/apk/res/android”
tools:context=”.MainActivity” >

android:text=”Android Notification Example” />

android:text=”Generate Notification” />


In this layout file we have used the relative layout and used one text view and one button. The java file is as follows:

package com.example.androidnotification;

import android.os.Bundle;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.support.v4.app.NotificationCompat;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity {
Button generateNotification;

protected void onCreate(Bundle savedInstanceState) {

generateNotification = (Button) findViewById(R.id.btnSend);

generateNotification.setOnClickListener(new OnClickListener() {

public void onClick(View arg0) {
// TODO Auto-generated method stub

public void getNotification(View view)
NotificationCompat.Builder ncb = new NotificationCompat.Builder(this);
ncb.setContentText(“here all the notes are available”);
Notification n = ncb.build();
NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

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;


In this java file, the following packages are used:


After that we have to declare the reference variable of Button class so that we can use the button defined in the xml file as follows: android

Here we have declared a button type variable called as geneeratNotification. After that we have to register this using the id of the button which is defined in the xml file as follows: android

After registering this, now we have to define the action when we click on the button. In java file, when we define the action for button then we have to use setOnClickListener() method. And inside this method, we have used another method called getNotification() that is user defined as follows: android

Inside this we have created the object of the NotificationCompat.Builder object and pass the current context means pass the this for the current activity.
After that we have set the icon for the notification using the ncb.setSmallIcon() method which takes one argument for icon. After that we have set the title for the notification using the ncb.setContentTitle() method which takes one string parameter for the title, and after that we have set the message using the ncb.setContentText() method which takes one parameter of type string for the message.
After that we have created the object of the Notification class and pass the reference of the build() method means it is used to set the notification. At this point, we have created the notification but now we have to show the notification. For this we have to create the variable of type NotificationManager and pass the reference of the getSystemService() method which takes one parameter of inbuilt string type.
After that we have to call the notify() method which takes two arguments : first argument for id of the notification of integer type and other argument is the object of the Notification class.
When the application will launch then it will look like as follows


When we will click on button then notification icon will be shown on the top of the screen as follows:


When we slide the notification bar then we can see the notification as follows:


Real Time Web Analytics