0

I'm sending a notification to the user with the following code using UNTimeIntervalNotificationTrigger. The notification is sent after 1 hour, that works. Now I want to give the user the ability to reset the TimeInterval for the notification so that the same notification runs again, but the TimeInterval only resets when the user presses this button. This means that repeats: true is not an option.

My code:

let tijd = 15

@IBAction func change(_ sender: Any) {
    // Notification
    let content = UNMutableNotificationContent()
    content.title = "title"
    content.body = "body"
    content.badge = 1
    content.sound = UNNotificationSound.default()


    // Timer
    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 repeat(_ sender: Any) {
    let trigger = UNTimeIntervalNotificationTrigger(timeInterval: TimeInterval(tijd), repeats: false)
    trigger.invalidate()
}

I tried invalidating the TimeInterval when clicking the button repeat, but this only gave me an error so I don't think this is the way to go. What is the way do preform this action? :)

rmaddy
  • 314,917
  • 42
  • 532
  • 579
G Buis
  • 303
  • 4
  • 17
  • what you want to do exactly ? – KKRocks Apr 24 '17 at 13:16
  • @KKRocks I want to reset the TimeInterval so that the notification will run again after the same time has passed for the previous one. I have something set up now but that is just the exact same code in the 2 buttons, I don't think this should be the solution..? – G Buis Apr 24 '17 at 13:21
  • first you need to removeAllPendingNotificationRequests and then post again new notification . – KKRocks Apr 24 '17 at 13:22
  • Okay, Thanks! I just don't understand why I would need to remove the pending notification because there is no notification after it's sent, right? – G Buis Apr 24 '17 at 13:50
  • because when you post same notification again then already posted notification also fire with its fire time . – KKRocks Apr 24 '17 at 13:52
  • is this working fine ? – KKRocks Apr 26 '17 at 12:07
  • It is! I'm can't just yet accept my answer below but I will in a few. Thanks for the help! – G Buis Apr 26 '17 at 12:09
  • I tried to do that, but couldn't find a similar way to say 'this is the answer'! If you want to you can even copy my code from below and I'll mark that as the answer! – G Buis Apr 26 '17 at 12:37
  • please tell me that essential part so i will add that part only my answer . – KKRocks Apr 26 '17 at 13:02
  • "First remove the old notification using the following code: `// Remove notification UNUserNotificationCenter.current().removePendingNotificationRequests(withIdentifiers: [bezigheid])` Then you can set up the next notification just like you set up the first notification!" - You can copy all this and I'll mark it as the answer! – G Buis Apr 26 '17 at 13:15

2 Answers2

0

It was quite simple, thanks to @KKRocks I was able to find a solution. I just had to remove it and add the same notification again, see the code:

let tijd = 15

@IBAction func change(_ sender: Any) {
    // Notification
    let content = UNMutableNotificationContent()
    content.title = "title"
    content.body = "body"
    content.badge = 1
    content.sound = UNNotificationSound.default()

    // Timer
    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 repeat(_ sender: Any) {     
    // Remove notification
    UNUserNotificationCenter.current().removePendingNotificationRequests(withIdentifiers: [bezigheid])

    // Notification
    let content = UNMutableNotificationContent()
    content.title = "title"
    content.body = "body"
    content.badge = 1
    content.sound = UNNotificationSound.default()

    // Timer
    let trigger = UNTimeIntervalNotificationTrigger(timeInterval: TimeInterval(tijd), repeats: false)
    let request = UNNotificationRequest(identifier: bezigheid, content: content, trigger: trigger)

    // Add notification
    UNUserNotificationCenter.current().add(request, withCompletionHandler: nil)
}
G Buis
  • 303
  • 4
  • 17
0

First remove the old notification using the following code: // Remove notification

UNUserNotificationCenter.current().removePendingNotification‌​Requests(withIdentif‌​iers: [bezigheid])

Then you can set up the next notification just like you set up the first notification!

KKRocks
  • 8,222
  • 1
  • 18
  • 84