我正在计划一个基于位置的UILocalNotification,点击一个按钮。但是,当我试图通过再次单击相同的按钮来取消localNotification时,它不会取消通知。我正在使用UIApplication.sharedApplication().cancelLocalNotification(localNotification)取消基于本地通知的预定位置。我做错什么了?这是我的实现
@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块代码
func locationManager(manager: CLLocationManager!, didEnterRegion region: CLRegion!) {
localNotification.regionTriggersOnce = true
localNotification.alertBody = "Stack Overflow is great"
UIApplication.sharedApplication().scheduleLocalNotification(localNotification)
NSLog("Entering region")
}发布于 2015-08-11 20:24:27
如果这在上下文中是可以接受的,您可以尝试删除所有通知。如下所示:
for notification in UIApplication.sharedApplication().scheduledLocalNotifications as! [UILocalNotification] {
UIApplication.sharedApplication().cancelLocalNotification(notification)
}或如Logan所述:
UIApplication.sharedApplication().cancelAllLocalNotifications()或者正如Gerard Grundy为Swift 4所指出的:
UNUserNotificationCenter.current().removeAllPendingNotificationRequests()发布于 2019-03-04 15:53:50
可以使用通知的标识符取消通知:
let center = UNUserNotificationCenter.current()
center.removeDeliveredNotifications(withIdentifiers: [String])
center.removePendingNotificationRequests(withIdentifiers: [String])发布于 2017-03-01 09:02:11
iOS 10+ Swift 3.1的解决方案
let center = UNUserNotificationCenter.current()
center.removeAllDeliveredNotifications() // To remove all delivered notifications
center.removeAllPendingNotificationRequests()https://stackoverflow.com/questions/31951142
复制相似问题