Prevent user from dismissing notification

AndroidNotificationsAndroid NotificationsDismiss

Android Problem Overview


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

How can I manage such behaviour?

Android Solutions


Solution 1 - Android

In addition to Andro Selvas answer:

If you are using the NotificationCompat.Builder, just use

builder.setOngoing(true);

Solution 2 - Android

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

Solution 3 - Android

I used the below code to make my notification persistent:

> startForeground(yourNotificationId,notificationObject);

To make it dismissable, just do the below:

> stopForeground(true);

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMarian KlühspiesView Question on Stackoverflow
Solution 1 - AndroidMarian KlühspiesView Answer on Stackoverflow
Solution 2 - AndroidAndro SelvaView Answer on Stackoverflow
Solution 3 - AndroidBodhisatwa ChakrabortyView Answer on Stackoverflow