Cancel a local notification and schedule a new one

大兔子大兔子 提交于 2019-12-12 04:10:42

问题


I have a problem. I have local notifications in my project, here is the code (thanks @leoDabus):

import UIKit

class ViewController: UIViewController {

@IBOutlet var datePicker: UIDatePicker!
@IBOutlet var notificationSwitch: UISwitch!

let localNotification = UILocalNotification()

override func viewDidLoad() {
    super.viewDidLoad()
    setUpNotificationsOptions()
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()

}
override func viewDidAppear(animated: Bool) {
    guard let loadedDate = NSUserDefaults().dateForKey("datePicker") else { return }
    datePicker.setDate(loadedDate, animated: false)
}
func setUpNotificationsOptions()  {
    datePicker.datePickerMode = .Time
    localNotification.timeZone = NSTimeZone.localTimeZone()
    localNotification.repeatInterval = .Day
    localNotification.alertAction = "Open App"
    localNotification.alertBody = news[0].titleNews
    localNotification.soundName = UILocalNotificationDefaultSoundName
}

func toggleNotification() {
    if notificationSwitch.on {
        localNotification.fireDate = datePicker.date.fireDate
        UIApplication.sharedApplication().scheduleLocalNotification(localNotification)
    } else {
        localNotification.fireDate = nil
        UIApplication.sharedApplication().cancelLocalNotification(localNotification)
    }
}
@IBAction func toggleSwitch(sender: UISwitch) {
  toggleNotification()
}
@IBAction func dateChanged(sender: UIDatePicker) {
   NSUserDefaults().setDate(sender.date, forKey: "datePicker")

   toggleNotification()
      } }


            extension NSUserDefaults {
   func setDate(date: NSDate, forKey:String) {
    NSUserDefaults().setObject(date, forKey: forKey)
}
func dateForKey(string:String) -> NSDate? {
    return NSUserDefaults().objectForKey(string) as? NSDate
}}

the problem is that my local notification alert Body news[0].titleNews is the result of a parser and it's a value that changes periodically. But with the code above i obtain every day the same string, not the updated one. There is a method to have everyday the updated news? Can I cancel last scheduled notification programmatically and scheduling new one?


回答1:


Set a unique id in the userInfo property of UILocalNotification and when you get the updated status/news, traverse throught all the scheduled notifications , get your notification with that id you set earlier and now do your work accordingly.

Set your id like the following

localNotification.userInfo = ["notificationID" : "Your Id"]

Code for traversing scheduled notifications

  let notificationArr:NSArray?  =  UIApplication.sharedApplication().scheduledLocalNotifications
  notificationArr!.enumerateObjectsUsingBlock({ object, index, stop in

          let notification = object as! UILocalNotification;
          let userInfo = notification.userInfo! as NSDictionary
          let notificationID = userInfo["notificationID"] as! String

         if(notificationID ==  "Your set Id"){

            UIApplication.sharedApplication().cancelLocalNotification(notification);
        }
    })


来源:https://stackoverflow.com/questions/34288785/cancel-a-local-notification-and-schedule-a-new-one

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!