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.
- Context context;
- final int NOTIFICATION_ID = 1;
Now make the object of the Notification Builder as in the following:
- context = NonRemovableNotification.this;
-
-
- Notification.Builder builder = new Notification.Builder(context);
Then set the title and the text and the small icon as in the following:
- builder.setContentText("there is a demo message").setContentTitle(
- context.getString(R.string.app_name));
- 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.
-
- Intent secondActivityIntent = new Intent(NonRemovableNotification.this,
- SecondaryActivity.class);
-
- PendingIntent pendingIntent = PendingIntent.getActivity(context, 0,
- secondActivityIntent, 0);
Set the pendingIntent into a Notification.Builder object.
- builder.setContentIntent(pendingIntent);
- builder.setAutoCancel(false);
Make the Notification Object and set the flag, this flag is the main code to make the non-removable notification.
- Notification notification = builder.build();
- notification.flags |= Notification.FLAG_NO_CLEAR;
Make the NotificationManager object and notify this with a Notification Id (int value) and Notification object.
- NotificationManager manager = (NotificationManager) context
- .getSystemService(Context.NOTIFICATION_SERVICE);
- manager.notify(NOTIFICATION_ID, notification);
Here is the full code of the Activity.
- package com.example.nonremovablenotification;
-
- import android.app.Activity;
- import android.app.Notification;
- import android.app.NotificationManager;
- import android.app.PendingIntent;
- import android.content.Context;
- import android.content.Intent;
- import android.os.Bundle;
-
- public class NonRemovableNotification extends Activity {
- Context context;
- final int NOTIFICATION_ID = 1;
-
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
-
- context = NonRemovableNotification.this;
-
- Notification.Builder builder = new Notification.Builder(context);
- builder.setContentText("there is a demo message").setContentTitle(
- context.getString(R.string.app_name));
- builder.setSmallIcon(R.drawable.ic_launcher);
-
- Intent secondActivityIntent = new Intent(NonRemovableNotification.this,
- SecondaryActivity.class);
-
- PendingIntent pendingIntent = PendingIntent.getActivity(context, 0,
- secondActivityIntent, 0);
-
- builder.setContentIntent(pendingIntent);
- builder.setAutoCancel(false);
- Notification notification = builder.build();
-
- notification.flags |= Notification.FLAG_NO_CLEAR;
- NotificationManager manager = (NotificationManager) context
- .getSystemService(Context.NOTIFICATION_SERVICE);
- manager.notify(NOTIFICATION_ID, notification);
-
- }
- }