Marian Klühspies
Marian Klühspies

Reputation: 17717

Prevent user from dismissing notification

Some apps have notifications which can´t be dismissed by swiping them away.

How can I manage such behaviour?

Upvotes: 81

Views: 36059

Answers (5)

IliyaTi
IliyaTi

Reputation: 461

Targetting Android 14+, setting ongoing to true doesn't stop the notification from getting dismissed anymore. Even critical Foreground services (like media playback) would get dismissed.

The only remaining solution would be having an Accessibility Service. Which I think you wouldn't want that. Since Google Play would not let it slip easily.

Upvotes: 0

Saurabh Kumar
Saurabh Kumar

Reputation: 2813

This is not an answer, just additional information on the above answers.

build.setOngoing(true) and FLAG_ONGOING_EVENT NO longer work on Android 14.

https://developer.android.com/about/versions/14/behavior-changes-all#non-dismissable-notifications

Upvotes: 13

I used the below code to make my notification persistent:

startForeground(yourNotificationId,notificationObject);

To make it dismissable, just do the below:

stopForeground(true);

Upvotes: 2

Marian Klühspies
Marian Klühspies

Reputation: 17717

In addition to Andro Selvas answer:

If you are using the NotificationCompat.Builder, just use

builder.setOngoing(true);

Upvotes: 156

Andro Selva
Andro Selva

Reputation: 54330

Use the flag,FLAG_ONGOING_EVENT to make it persistent.

Notification notification = new Notification(icon, tickerText, when);
    notification.flags = Notification.FLAG_ONGOING_EVENT;

Also you can check, FLAG_NO_CLEAR

Upvotes: 44

Related Questions