简体   繁体   中英

Android: Delete Notification only when touched not throuch “clear all” Button

my App creates a few notifications and with the Help of PendingIntents it takes you to an special Activity. It all works pretty fine, the only problem i have is that i want the Notification to only be deleted when the user touches it, not when the user uses the "Clear all" Button ( X on top of the Notificationbar).

At the Moment i am using the

setAutoCancel(true);

method. It deletes the Notification when the user touches it, but also when the "Clear ALl" Button is pressed. My Question is now if theres any way i can surpress the Notification being deleted by the "Clear All" Button.

If you wonder why i want this, i created a App thats allows you to use Notifications as an kind of reminder. The "Clear Button" is annoying here then, because it deletes all the reminder notifications.

Thx for the Help, if you want any additional Code, just write it.

You can accomplish this by setting your Notification as ongoing, and then cancelling it when the PendingIntent fires.

Ongoing notifications cannot be dismissed by the user, so your application or service must take care of canceling them.

Assuming you're using a Notification.Builder to create your Notification, simply add the following line before calling build() .

.setOngoing(true)

When you wish to remove the Notification, call cancel() on the NotificationManager Service with the same ID used when posting the Notification.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM