Prevent user from dismissing notification
Asked Answered
A

4

78

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

How can I manage such behaviour?

Aluino answered 27/8, 2013 at 12:1 Comment(0)
A
156

In addition to Andro Selvas answer:

If you are using the NotificationCompat.Builder, just use

builder.setOngoing(true);
Aluino answered 1/12, 2015 at 23:28 Comment(4)
I prefer this answer more, just because I am using Builder.Viscount
This does not work to me. It looks like it makes the notification without a "x" close button, but the swipe gesture can still be done.Heathheathberry
Worked for me using NotificationCompat builder.Selma
This is not working on Huawei phones, but working on SamsungGlyco
G
43

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

Gauleiter answered 27/8, 2013 at 12:2 Comment(1)
I tried to using "setOngoing(true)", but it doesn't work for me. And "FLAG_ONGOING_EVENT" work for me.Ouachita
F
7

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

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

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

Frivol answered 12/1 at 9:3 Comment(0)
D
2

I used the below code to make my notification persistent:

startForeground(yourNotificationId,notificationObject);

To make it dismissable, just do the below:

stopForeground(true);

Donnadonnamarie answered 1/8, 2018 at 4:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.