Introduction

Hi friends. This is another of my articles on notifications in Android.
First, we must understand what notifications are in Android. A notification 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 useful 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. import android.app.Activity;
  3. import android.app.Notification;
  4. import android.app.NotificationManager;
  5. import android.app.PendingIntent;
  6. import android.content.Context;
  7. import android.content.Intent;
  8. import android.os.Bundle;
  9. public class NonRemovableNotification extends Activity {
  10. Context context;
  11. final int NOTIFICATION_ID = 1;
  12. @Override
  13. protected void onCreate(Bundle savedInstanceState) {
  14. super.onCreate(savedInstanceState);
  15. context = NonRemovableNotification.this;
  16. // create notification builder object
  17. Notification.Builder builder = new Notification.Builder(context);
  18. builder.setContentText("there is a demo message").setContentTitle(
  19. context.getString(R.string.app_name));
  20. builder.setSmallIcon(R.drawable.ic_launcher);
  21. // make the intent object
  22. Intent secondActivityIntent = new Intent(NonRemovableNotification.this,
  23. SecondaryActivity.class);
  24. // pending intent
  25. PendingIntent pendingIntent = PendingIntent.getActivity(context, 0,
  26. secondActivityIntent, 0);
  27. builder.setContentIntent(pendingIntent);
  28. builder.setAutoCancel(false);
  29. Notification notification = builder.build();
  30. // this is the main thing to do to make a non removable notification
  31. notification.flags |= Notification.FLAG_NO_CLEAR;
  32. NotificationManager manager = (NotificationManager) context
  33. .getSystemService(Context.NOTIFICATION_SERVICE);
  34. manager.notify(NOTIFICATION_ID, notification);
  35. }
  36. }