使用计时器计划通知触发

时间:2018-09-13 04:11:19

标签: ios swift notifications nstimer

我正在尝试根据用户在UIDatePicker中选择的内容,安排一个计时器在特定的日期和时间触发。计时器触发时,我想每60秒设置一次重复通知(UNTimeIntervalNotificationTrigger)。计时器似乎启动,控制台显示正在添加通知而没有错误,但是我从未收到通知。我在做什么错了?

@IBAction func triggerNotification(_ sender: Any) {
    if (reminderText.text!.count > 0)
    {
        let timer = Timer(fireAt: datePicker.date, interval: 0, target: self, selector: #selector(setUpReminder), userInfo: nil, repeats: false)
        RunLoop.main.add(timer, forMode: RunLoopMode.commonModes)
        self.dismiss(animated: true, completion: nil)
        reminderText.text = ""
    }
}
@objc func setUpReminder()
{
    let content = UNMutableNotificationContent()
    let identifier = reminderText.text!
    content.title = "Your Reminder"
    content.body = identifier
    content.badge = 1
    let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 60.0, repeats: true)
    let request = UNNotificationRequest(identifier: identifier, content: content, trigger: trigger)
    UNUserNotificationCenter.current().add(request){
        (error) in
        if error != nil
        {
            print("here error in setting up notification")
            print(error!)
        } else
        {
            print("notification scheduled")
        }
    }
}

1 个答案:

答案 0 :(得分:0)

var jsonEntDataIP = jsonEntData["IP"] ?? '';
  

这是工作代码,您只是在分配错误的timeinterval:)