Я пытаюсь запланировать запуск таймера на определенную дату и время в зависимости от того, что пользователь выбирает в UIDatePicker. Когда срабатывает таймер, я хочу настроить повторяющееся уведомление (UNTimeIntervalNotificationTrigger) каждые 60 секунд. Таймер, кажется, срабатывает, и консоль показывает, что уведомление добавляется без ошибок, но я никогда не получаю уведомление. Что я делаю не так?
@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")
}
}
}