为什么取消本地通知会触发重复的本地通知(产生本地通知)?

时间:2015-12-09 18:23:27

标签: ios notifications

我有一个iOS应用,其中包含(每日)重复的本地通知,当some条件满足时会触发新的本地通知。必须在应用中显示的本地通知(带横幅)和notifications view内的通知。我已经在AppDelegate中大致实现了它作为概念证明并且有效(tm)。

从横幅广告块中的onetime notifications view cancelLocalNotification移除UIApplication.sharedApplication().cancelLocalNotification(<the one time notification>)本地通知。但令人惊讶的是,在发射... import JCNotificationBannerPresenter ... class AppDelegate: UIResponder, UIApplicationDelegate { func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { // Setup local notifications support let notificationType: UIUserNotificationType = [UIUserNotificationType.Alert, UIUserNotificationType.Badge, UIUserNotificationType.Sound] let settings = UIUserNotificationSettings(forTypes: notificationType, categories: nil) application.registerUserNotificationSettings(settings) // Clean up and reset application.applicationIconBadgeNumber = 0 application.cancelAllLocalNotifications() NSLog("Setup one repeating notification") let n = UILocalNotification() n.fireDate = NSDate(timeIntervalSinceNow: 15) n.soundName = UILocalNotificationDefaultSoundName n.timeZone = NSTimeZone.defaultTimeZone() n.applicationIconBadgeNumber = 0 n.userInfo = ["id": "dailyreminder"] n.repeatInterval = NSCalendarUnit.Day UIApplication.sharedApplication().scheduleLocalNotification(n) return true } ... func application(application: UIApplication, didReceiveLocalNotification notification: UILocalNotification) { NSLog("application(... didReceiveLocalNotification/////////////////////////") NSLog(notification.description) let state = application.applicationState if let userInfo = notification.userInfo, let id = userInfo["id"] as? String where id != "dailyreminder" { switch state { case UIApplicationState.Active: NSLog(String(format: "%@ > UIApplicationState.Active", id)) JCNotificationCenter.enqueueNotificationWithTitle( notification.alertTitle, message: notification.alertBody, tapHandler: { () -> Void in NSLog("Banner click ///////////////////////////////////////////////") // 1. Go to somewhere in the app // 2. Cancel the notification from the `notifications view` UIApplication.sharedApplication().cancelLocalNotification(notification) // `cancel` results in an additional call up of the "dailyreminder" ~ why? }) case UIApplicationState.Inactive: NSLog(String(format: "%@ > UIApplicationState.Inactive", id)) // This is called up when user selects the notification within the `notifications view` // and canceled by user touch. default: let desc = state.rawValue.description NSLog(String(format: "%@ > Unexpected app state: %@", id, desc)) } } if let userInfo = notification.userInfo, let id = userInfo["id"] as? String where id == "dailyreminder" { NSLog("Repeating daily reminder was called ~ lets fire a noti to show the user") // Some condition checks that need to be true before firing the local notification if true { let n = UILocalNotification() n.fireDate = nil n.timeZone = NSTimeZone.defaultTimeZone() n.applicationIconBadgeNumber = ++UIApplication.sharedApplication().applicationIconBadgeNumber n.userInfo = [ "id": "onetimenotification" ] n.alertTitle = "One time notification title" n.alertBody = "One time notification body" UIApplication.sharedApplication().scheduleLocalNotification(n) } } } ... 时会重新触发重复的本地通知 - 为什么?我该如何防止这种情况?

case UIApplicationState.Active:
    NSLog(String(format: "%@ > UIApplicationState.Active", id))
    UIApplication.sharedApplication().cancelLocalNotification(notification)

更新1

我在github上提供了完整的示例。

更新2

好的我给了它另一个拍摄。在块外面取消通知有效(但不适合该场景):

JCNotificationBannerPresenter

我也想消除case UIApplicationState.Active: NSLog(String(format: "%@ > UIApplicationState.Active", id)) let alertController = UIAlertController( title: "Daily reminder title", message: "Daily reminder description.", preferredStyle: .Alert) let cancelAction = UIAlertAction(title: "Cancel", style: .Cancel) { _ in NSLog("Do nothing ////////////////////// ") } alertController.addAction(cancelAction) let OKAction = UIAlertAction(title: "OK", style: .Default) { (action) in NSLog("OK was pressed ////////////// ") UIApplication.sharedApplication().cancelLocalNotification(notification) } alertController.addAction(OKAction) self.window!.makeKeyAndVisible() let v = self.window!.rootViewController! v.presentViewController(alertController, animated: true, completion: {}) 导致奇怪的行为,所以我尝试了新的ios altertController:

UIApplication.sharedApplication().cancelLocalNotification(notification)

遗憾的是,它重现了这种奇怪的行为,所以它的味道就像块与{{1}}的组合......

1 个答案:

答案 0 :(得分:0)

您尚未在didReceiveLocalNotification方法中取消重复通知,即每日提醒通知。您已取消通知where id != "dailyreminder",而您重复通知的时间为id == "dailyreminder"。在didReceiveLocalNotification

中查看您的条件