简体   繁体   中英

Allow users to disable notifications Android

I don't know if this can be done but I have a scheduler/diary app that allows the user to add/remove items. When the user adds an item, they are given an option to set a notification for either 1 hour, 30 mins, or 15 mins before the event time or else to not be notified. I am using an alarm manager with a broadcast receiver to handle this.

I want to display an option in the app menu to enable/disable notifications; ie if an alarm is scheduled to sound in 15 mins it should not happen. How can I implement this?

Take a look at NotificationManager class. When you set your notification you can provide a notification id like this:

mNotificationManager.notify(id, notification)

If you later want to cancel this notification(ie like in your case, if a user has selected an option to disable notifications) you would need to use

mNotificationManager.cancel(id)

Or you could use cancelAll() to cancel all notifications:

mNotificationManager.cancelAll ()

Let me know if this helps.

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