Non-Removable Notifications in Android

Hi friends. This is another of my articles on notifications in Android.

First we must understand what notifications are in Android. A notifiction is a message in Android to show the update on the top of the title bar and notification slide.

To make a Permanent Notification we must use the following few steps.

First make the Notification ID. This is very usefull for showing the notification, using this id we can easily remove the notification programmatically.

  1. Context context;  
  2. final int NOTIFICATION_ID = 1;  
Now make the object of the Notification Builder as in the following:
  1. context = NonRemovableNotification.this;  
  2. //here NonRemovableNotification is an Activity  
  3. // create notification builder object  
  4. Notification.Builder builder = new Notification.Builder(context);  
Then set the title and the text and the small icon as in the following:
  1. builder.setContentText("there is a demo message").setContentTitle(  
  2. context.getString(R.string.app_name));  
  3. builder.setSmallIcon(R.drawable.ic_launcher);  
Then, make the Intent to open a new activity on the tap on the Notification and set this intent into PendingIntent for further use.
  1. // make the intent object  
  2. Intent secondActivityIntent = new Intent(NonRemovableNotification.this,  
  3. SecondaryActivity.class);  
  4. // pending intent  
  5. PendingIntent pendingIntent = PendingIntent.getActivity(context, 0,  
  6. secondActivityIntent, 0);  
Set the pendingIntent into a Notification.Builder object.
  1. builder.setContentIntent(pendingIntent);  
  2. builder.setAutoCancel(false);  
Make the Notification Object and set the flag, this flag is the main code to make the non-removable notification.
  1. Notification notification = builder.build();  
  2. notification.flags |= Notification.FLAG_NO_CLEAR;  
Make the NotificationManager object and notify this with a Notification Id (int value) and Notification object.
  1. NotificationManager manager = (NotificationManager) context  
  2. .getSystemService(Context.NOTIFICATION_SERVICE);  
  3. manager.notify(NOTIFICATION_ID, notification);  
android

Here is the full code of the Activity.
  1. package com.example.nonremovablenotification;  
  2.   
  3. import android.app.Activity;  
  4. import android.app.Notification;  
  5. import android.app.NotificationManager;  
  6. import android.app.PendingIntent;  
  7. import android.content.Context;  
  8. import android.content.Intent;  
  9. import android.os.Bundle;  
  10.   
  11. public class NonRemovableNotification extends Activity {  
  12.     Context context;  
  13.     final int NOTIFICATION_ID = 1;  
  14.   
  15.     @Override  
  16.     protected void onCreate(Bundle savedInstanceState) {  
  17.         super.onCreate(savedInstanceState);  
  18.           
  19.         context = NonRemovableNotification.this;  
  20.         // create notification builder object  
  21.         Notification.Builder builder = new Notification.Builder(context);  
  22.         builder.setContentText("there is a demo message").setContentTitle(  
  23.                 context.getString(R.string.app_name));  
  24.         builder.setSmallIcon(R.drawable.ic_launcher);  
  25.         // make the intent object  
  26.         Intent secondActivityIntent = new Intent(NonRemovableNotification.this,  
  27.                 SecondaryActivity.class);  
  28.         // pending intent  
  29.         PendingIntent pendingIntent = PendingIntent.getActivity(context, 0,  
  30.                 secondActivityIntent, 0);  
  31.   
  32.         builder.setContentIntent(pendingIntent);  
  33.         builder.setAutoCancel(false);  
  34.         Notification notification = builder.build();  
  35.         // this is the main thing to do to make a non removable notification  
  36.         notification.flags |= Notification.FLAG_NO_CLEAR;  
  37.         NotificationManager manager = (NotificationManager) context  
  38.                 .getSystemService(Context.NOTIFICATION_SERVICE);  
  39.         manager.notify(NOTIFICATION_ID, notification);  
  40.   
  41.     }  
  42. }

 

Up Next
    Ebook Download
    View all
    Learn
    View all