取消本地通知并安排新通知 [英] Cancel a local notification and schedule a new one

查看:27
本文介绍了取消本地通知并安排新通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有问题.我的项目中有本地通知,这是代码(感谢@leoDabus):

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
}}

问题是我的本地通知警报 Body news[0].titleNews 是解析器的结果,它是一个定期更改的值.但是使用上面的代码,我每天都会获得相同的字符串,而不是更新的字符串.有没有每天更新新闻的方法?我可以以编程方式取消上次安排的通知并安排新的通知吗?

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?

推荐答案

UILocalNotificationuserInfo 属性中设置唯一 id,并在您获得更新的状态/新闻时,遍历所有预定的通知,使用您之前设置的 ID 获取通知,然后相应地完成您的工作.

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.

如下设置你的id

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

遍历预定通知的代码

  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);
        }
    })

这篇关于取消本地通知并安排新通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆