首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何快速按下按钮取消localNotification?

如何快速按下按钮取消localNotification?
EN

Stack Overflow用户
提问于 2015-08-11 20:14:55
回答 6查看 23.3K关注 0票数 22

我正在计划一个基于位置的UILocalNotification,点击一个按钮。但是,当我试图通过再次单击相同的按钮来取消localNotification时,它不会取消通知。我正在使用UIApplication.sharedApplication().cancelLocalNotification(localNotification)取消基于本地通知的预定位置。我做错什么了?这是我的实现

代码语言:javascript
复制
@IBAction func setNotification(sender: UIButton!) {
    if sender.tag == 999 {
        sender.setImage(UIImage(named: "NotificationFilled")!, forState: .Normal)
        sender.tag = 0
        regionMonitor() //function where notification get scheduled

    } else {
        sender.setImage(UIImage(named: "Notification")!, forState: .Normal)
        sender.tag = 999 }

为了取消计划的通知,我应该输入什么其他块。无法清除所有通知。下面是触发本地通知的didEnterRegion块代码

代码语言:javascript
复制
func locationManager(manager: CLLocationManager!, didEnterRegion region: CLRegion!) {
    localNotification.regionTriggersOnce = true
    localNotification.alertBody = "Stack Overflow is great"
    UIApplication.sharedApplication().scheduleLocalNotification(localNotification)
    NSLog("Entering region")
}
EN

回答 6

Stack Overflow用户

回答已采纳

发布于 2015-08-11 20:24:27

如果这在上下文中是可以接受的,您可以尝试删除所有通知。如下所示:

代码语言:javascript
复制
for notification in UIApplication.sharedApplication().scheduledLocalNotifications as! [UILocalNotification] { 
  UIApplication.sharedApplication().cancelLocalNotification(notification)
}

或如Logan所述:

代码语言:javascript
复制
UIApplication.sharedApplication().cancelAllLocalNotifications()

或者正如Gerard Grundy为Swift 4所指出的:

代码语言:javascript
复制
UNUserNotificationCenter.current().removeAllPendingNotificationRequests()
票数 58
EN

Stack Overflow用户

发布于 2019-03-04 15:53:50

可以使用通知的标识符取消通知:

代码语言:javascript
复制
let center = UNUserNotificationCenter.current()
center.removeDeliveredNotifications(withIdentifiers: [String])
center.removePendingNotificationRequests(withIdentifiers: [String])
票数 8
EN

Stack Overflow用户

发布于 2017-03-01 09:02:11

iOS 10+ Swift 3.1的解决方案

代码语言:javascript
复制
let center = UNUserNotificationCenter.current()
center.removeAllDeliveredNotifications() // To remove all delivered notifications
center.removeAllPendingNotificationRequests()
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31951142

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档