0

I'm trying to reset a UNTimeIntervalNotificationTrigger to sent a notification multiple times. I've tried a few things, including:

-Set repeats: true -> This crashes my application somehow, giving me the error thread 1 signal sigabrt

Now the problem with the current code (see below) is that I get the error: Use of unresolved identifier 'trigger'. Now for as far as I understand this error shows me that I can't use trigger because it is set in a button 'function' and that it can't find let trigger.

Question: How do I make the function resetTimer see trigger? Or is there a better way to reset UNTimeIntervalNotificationTrigger?

@IBAction func change(_ sender: Any) {
func startTimer() {
        let trigger = UNTimeIntervalNotificationTrigger(timeInterval: TimeInterval(tijd), repeats: false)
        let request = UNNotificationRequest(identifier: bezigheid, content: content, trigger: trigger)

        UNUserNotificationCenter.current().add(request, withCompletionHandler: nil)
    }
}

func resetTimer() {
    trigger.invalidate
}
rmaddy
  • 314,917
  • 42
  • 532
  • 579
G Buis
  • 303
  • 4
  • 17

1 Answers1

0

First of all, this code is completely incorrect. It's not ment to put functions inside @IBActions.
Do it like this:

@IBAction func startTimer(_ sender: Any) {
    let trigger = UNTimeIntervalNotificationTrigger(timeInterval: TimeInterval(tijd), repeats: false)
    let request = UNNotificationRequest(identifier: bezigheid, content: content, trigger: trigger)

    UNUserNotificationCenter.current().add(request, withCompletionHandler: nil)
}

@IBAction func resetTimer(_ sender: Any) {
    let trigger = UNTimeIntervalNotificationTrigger(timeInterval: TimeInterval(tijd), repeats: false)
    trigger.invalidate
}
  • Hi, thank you for your reply. Although it doesn't really work. The error I get is: `Value of type 'UNTimeIntervalNotificationTrigger' has no member 'invalidate'` (pointing at `trigger.invalidate`) Also, can I start the timer while pressing the second (resetTimer) button? Because now it only resets the timer, right? – G Buis Apr 13 '17 at 14:27
  • This means that the function "invalidate" does not exists. – Matthijs Otterloo Apr 13 '17 at 14:33
  • So then I add it, how? `func invalidate { //Something? }` and then `trigger.invalidate()` ? – G Buis Apr 13 '17 at 14:36
  • You can add that function above the @IBAction and then use something like self.invalidate. – Matthijs Otterloo Apr 13 '17 at 14:38
  • The problem is that I don't know what to put in the invalidate function because usually (?) it just worked when I set it to `timer.invalidate` in that case. Can you maybe sent the code as an edit of your answer? If it works I can mark it as solved! – G Buis Apr 13 '17 at 14:39
  • Yes because .invalidate is not a function. What do you want to reach by using the .invalidate function? – Matthijs Otterloo Apr 13 '17 at 14:41
  • Because if you want to remove it you should use something like: `UNUserNotificationCenter.current().removePendingNotificationRequests` – Matthijs Otterloo Apr 13 '17 at 14:42
  • So I want this notification to run again when the user taps a certain button, meaning that the button should reset the trigger, right? (UNTimeIntervalWithNotification) The time interval is 3 days so when pressing the button the notification should pop up in 3 days again, and again 3 days later when the user presses the button. – G Buis Apr 13 '17 at 14:44
  • Do you think you can help me out? – G Buis Apr 13 '17 at 16:09