Android Notification

Notifications are really useful to display message to the user outside of your application. The notification also attracts the user to use your app. In this session we will create our own notification, come lets see how notification can be created.
To create notification, just follow below four steps:

  1. Create notification builder
  2. Defining notification properties
  3. Add actions to the notification
  4. Notify the notification

Let’s see every steps.

Step 1: Create notification builder
To create notification builder, we use NotificationCompat to create Builder object, as given below.

NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)

Step 2: Defining notification properties
Now to add properties to the notification, we have some methods. Few of them are implemented below.

mBuilder.setSmallIcon(R.drawable.notification_icon);
mBuilder.setContentTitle("Notification Title");
mBuilder.setContentText("This is Android Notification Detail!");

Step 3: Add actions to the notification
Now what should happen when user clicks the notification, where should the user be routed; all these things we define all the actions here. The action is defined by a PendingIntent containing an Intent that starts an Activity in your application. If you want to start Activity when the user clicks the notification text in the notification drawer, you add the PendingIntent by calling setContentIntent().

Intent resultIntent = new Intent(this, ResultActivity.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
stackBuilder.addParentStack(ResultActivity.class);
stackBuilder.addNextIntent(resultIntent);

PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0,PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(resultPendingIntent);

Step 4: Notify the notification

Finally, you pass the Notification object to the system by calling NotificationManager.notify() to send your notification.

NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(notificationID, mBuilder.build());

Now let’s see an example:

Below is the structure of our Android project.

androidnotificationfile
Layout file: activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     xmlns:tools="http://schemas.android.com/tools"
     android:id="@+id/activity_main"
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     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.wordpress.pawanthepro.notificationexample.MainActivity"
     android:orientation="vertical"
     android:background="@color/colorPrimary">
 
     <EditText
         android:id="@+id/title"
         android:hint="Title"
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         />
     <EditText
         android:id="@+id/text"
         android:hint="Text"
         android:layout_width="match_parent"
         android:layout_height="wrap_content" />
     <Button
         android:id="@+id/getNotification"
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:text="Get Notification"/>
 

Brain file: MainActivity.java

package com.wordpress.pawanthepro.notificationexample;
 
 import android.app.NotificationManager;
 import android.app.PendingIntent;
 import android.content.Context;
 import android.content.Intent;
 import android.support.v4.app.TaskStackBuilder;
 import android.support.v7.app.AppCompatActivity;
 import android.os.Bundle;
 import android.support.v7.app.NotificationCompat;
 import android.view.View;
 import android.widget.Button;
 import android.widget.EditText;
 
 public class MainActivity extends AppCompatActivity implements View.OnClickListener {
 
     EditText title;
     EditText text;
     Button getNotification;
 
     @Override
     protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.activity_main);
 
         title=(EditText) findViewById(R.id.title);
         text=(EditText) findViewById(R.id.text);
 
         getNotification= (Button) findViewById(R.id.getNotification);
 
         getNotification.setOnClickListener(this);
     }
 
     @Override
     public void onClick(View v) {
         if(v==getNotification){
             String titleContent=title.getText().toString();
             String textContent=text.getText().toString();
 
             NotificationCompat.Builder builder=new NotificationCompat.Builder(this);
             builder.setSmallIcon(R.drawable.notification_img);
             builder.setContentTitle(titleContent);
             builder.setContentText(textContent);
 
             Intent resultIntent = new Intent(this, Main2Activity.class);
             TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
             stackBuilder.addParentStack(Main2Activity.class);
             stackBuilder.addNextIntent(resultIntent);
 
             PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0,PendingIntent.FLAG_UPDATE_CURRENT);
             builder.setContentIntent(resultPendingIntent);
 
             NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
             mNotificationManager.notify(1, builder.build());
 
         }
     }
 }

Layout file: activity_main2.xml

 <?xml version="1.0" encoding="utf-8"?>
 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
     xmlns:tools="http://schemas.android.com/tools"
     android:id="@+id/activity_main2"
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     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.wordpress.pawanthepro.notificationexample.Main2Activity"
     android:background="@color/colorPrimary">
 
     <TextView
         android:text="Navigated Because of Notification"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_centerInParent="true"
         android:textColor="@android:color/white"
         tools:textSize="18sp" />
 
 RelativeLayout>

The output application’s screen capture:

Before clicking the button:

AndroidNotificationBeforeClick.png

The notification bar after button click and after notification click the app goes to second layout.

androidnotificationafterclick