Home Ask Login Register

Developers Planet

Your answer is one click away!

Vernon Kawonza February 2016

Prevent notification from being swiped off tray

Is it possible to prevent a notification from being swiped out of the notification tray? Like the usb notifications. If not, is there some sort of listener one can implement for that scenario?


Aziz February 2016

You can do this by making your notifications sticky like this

private void makeNotification(Context context) {
Intent intent = new Intent(context, MainActivity.class);

PendingIntent pendingIntent = PendingIntent.getActivity(context,

Notification.Builder builder = new Notification.Builder(context)
    .setContentTitle("Notification Title")
    .setContentText("Sample Notification Content")
    .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher))
Notification n;

    n = builder.build();
} else {
    n = builder.getNotification();

n.flags |= Notification.FLAG_NO_CLEAR | Notification.FLAG_ONGOING_EVENT;

notificationManager.notify(NOTIFICATION_ID, n);


Vyacheslav February 2016

Of course, this is possible My notification class:

public class ONots {

    public static Notification notification(Context ctx, String title, int icon,
            long when, PendingIntent pIntent, String contentTitle,
            String contentText,int flags, int defaults){
        NotificationCompat.Builder builder = new NotificationCompat.Builder(ctx);
        Notification notification = builder.setContentIntent(pIntent)
        notification.flags = flags;
        return notification;

somewhere in the code:

int defaults = Notification.DEFAULT_ALL;
        int flags = Notification.FLAG_NO_CLEAR;

Notification notif = ONots.notification(this, shown_top, ico, System.currentTimeMillis(), pIntent, contentTitle, contentText, flags, defaults);

            nm.notify(_.NAKSNOT, notif);

rupesh jain February 2016

You should set FLAG_ONGOING_EVENT.When you set this flag user can't dismiss your notification. For more info refer here

Constant Value: 32 (0x00000020)

public static final int FLAG_ONGOING_EVENT

Added in API level 1

Bit to be bitwise-ored into the flags field that should be set if this notification is in reference to something that is ongoing, like a phone call. It should not be set if this notification is in reference to something that happened at a particular point in time, like a missed phone call.

Constant Value: 2 (0x00000002)

Post Status

Asked in February 2016
Viewed 2,605 times
Voted 10
Answered 3 times


Leave an answer

Quote of the day: live life