Cancel a local notification and schedule a new one

2019-09-05 07:20发布

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条回答
Deceive 欺骗
2楼-- · 2019-09-05 08:10

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);
        }
    })
查看更多
登录 后发表回答