简体   繁体   English

允许用户禁用Android通知

[英]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. 当用户添加项目时,他们可以选择在事件时间之前的1小时,30分钟或15分钟内设置通知,否则将不被通知。 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. 即,如果计划在15分钟内响起警报,则不应发生。 How can I implement this? 我该如何实施?

Take a look at NotificationManager class. 看一下NotificationManager类。 When you set your notification you can provide a notification id like this: 设置通知时,您可以提供如下通知ID:

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: 或者,您可以使用cancelAll()取消所有通知:

mNotificationManager.cancelAll ()

Let me know if this helps. 让我知道是否有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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