Signup/Sign In

Android Notifications

Introduction

A notification is nothing but a message you can display to the user outside of your Android application. Even though the user is not using your app, they can receive the notifications of your app. Notifications can also be clicked to perform an action or to open a new activity.

Most of the times, whenever we open our mobile, we see a lot of small icons at the top.

Android Notifications

This area, where we see the small icons of the apps are known as notifications of the apps and the place at the top where it is displayed is called as notification area. This is the place from where we know that we have received a notification.

When we drag down the screen from the notification area, we open a notification drawer. This is the place where you can get more information about the notification of the app you have received.

Android Notifications

The advantage of using notification is that it does not distract a user from the current task, and still provides an option to respond to a notification by opening the notification drawer anytime.

The notification can be used to show a miss call, completion of a download, update of any app or any other app-specific event.


Creating Android Notification

There are two type of notifications - Normal View and Big View.

Android Notifications

Normal View Notification

Android Notifications

Big View Notification

Implementing a notification starts with creating the template of a notification, followed by defining the navigation from the notification i.e what will happen if we touch the notification and finally issuing the notification in the system.


Normal View Notification

Android Notifications

  1. Creating the template of the notification

    A normal view notification contains 3 necessary things - Icon, Content Title and Content Text. Therefore, to build a notification, we need a NotificationCompat.Builder object. This object is used to set the icon, title, text and various other properties of the notification.

    NotificationCompat.Builder b = new NotificationCompat.Builder(this);
    • setSmallIcon() method is used to set the icon to be shown in the notification.
    • setContentTitle() method is used to set the title to be shown in the notification.
    • setContentText() method is used to show the text/message in the notification.
    b.setSmallIcon(R.drawable.st);
    b.setContentTitle("New Notification");
    b.setContentText("Learning android notification from Studytonight");

    Therefore, by following the above steps, we have made our template of the notification.

  2. Defining the navigation from the notification

    To define which Activity to be opened or an action to be performed on clicking the notification, we use an object of PendingIntent class. PendingIntent wraps an object of Intent class. To open a specific activity, it uses getActivity() method which has 4 parameters

    • Context c - It is the reference to the class in which PendingIntent starts the activity.
    • int requestCode - requestCode is used to retrieve the same pending intent instance later on(for cancelling, etc) . 1 indicates a private request code for the sender.
    • Intent i - PendingIntent wraps this Explicit Intent object which is to be launched on clicking the notification.
    • int flag - This may be FLAG_ONE_SHOT, FLAG_NO_CREATE, FLAG_CANCEL_CURRENT, FLAG_UPDATE_CURRENT, or any of the flags as supported by Intent.fillIn() to control which unspecified parts of the intent that can be supplied when the actual send happens.
  3. Finally, the operation associated with the pendingIntent is executed using setContentIntent() method.

    Intent i = new Intent(this,SecondActivity.class);
    PendingIntent intent = PendingIntent.getActivity(this,1,i,PendingIntent.FLAG_UPDATE_CURRENT);
    b.setContentIntent(intent);
  4. Issuing the notification

    Finally to issue a notification to the system, we need an instance of NotificationManager class. We get this by calling getSystemService(Context.NOTIFICATION_SERVICE) method with the given parameter. We need to type-caste the Object returned with NotificationManager.

    Then we call the notify() method to issue the notification. When you call notify() method, you need to specify a notification ID and the Notification object. The ID is used to update the notification later on.

    To get the Notification object containing the specifications defined in the template and the action it will perform, we call the build() method.

    NotificationManager nm =(NotificationManager) getSystemService (Context.NOTIFICATION_SERVICE);
    nm.notify(0,b.build());

  5. Android Code

    activity_main.xml

    <?xml version="1.0" encoding="utf-8"?>
      <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:app="http://schemas.android.com/apk/res-auto"
          xmlns:tools="http://schemas.android.com/tools"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          tools:context="com.example.studytonight.notification.MainActivity">
    
          <TextView
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:text="See the notification Area, Your notification is waiting for you"
              android:textSize="30sp"
              android:textStyle="bold|italic"
              />
    
      </LinearLayout>
    

    MainActivity.java

    package com.example.aasiq.notification;
    
      import android.app.NotificationManager;
      import android.app.PendingIntent;
      import android.content.Context;
      import android.content.Intent;
      import android.graphics.Color;
      import android.support.v7.app.AppCompatActivity;
      import android.os.Bundle;
      import android.support.v7.app.NotificationCompat;
    
      public class MainActivity extends AppCompatActivity {
    
          @Override
          protected void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.activity_main);
    
    
      //Part 1: Creating the template of the notification
            NotificationCompat.Builder b = new NotificationCompat.Builder(this);
    
              b.setSmallIcon(R.drawable.ic_launcher);
              b.setContentTitle("This is the Notification Title");
              b.setContentText("I am the message");
    
      //Part 2: Defining the navigation from the notification
    
              PendingIntent intent = PendingIntent.getActivity(this,1,new Intent(this,MainActivity.class),
              PendingIntent.FLAG_UPDATE_CURRENT);
              b.setContentIntent(intent);
    
              //Part 3: Issuing the notification
              NotificationManager nm =(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
              nm.notify(0,b.build());
          }
      }

    You can download the whole project here