3

first I'd like to say that I've been searching for the answer to my question quite a bit but the only things I've found so far are answers for older versions of Swift or answers that don't specifically answer my question.

Background info:
I'm trying to develop an app that can remind you in a set interval. Now this works, given that you only set 1 reminder. However if I set the interval to be 20 seconds, launch the app, set 2 notifications and close the app only the second notification shows in 20 seconds. The first notification is being overwritten by the second one.

Question: How can I make sure that all of my notifications, requested by the user, actually get sent and that no notification overrides the previous one?

Code for the notification:

    let tijd = 20 //20 is just for the test, normally there is more code to it

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

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

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

This code is stored in a UITableView cell. My storyboard

Rajan Maheshwari
  • 14,465
  • 6
  • 64
  • 98
G Buis
  • 303
  • 4
  • 17
  • How are you setting both the notifications? Show that code. In the code provided, only a single notification is added to the center. – Rajan Maheshwari Apr 13 '17 at 08:37
  • Oh right, then I think that that might be the problem. What I have now is: I tap a cell and the second VC appears using the provided code for every single cell. Should I change anything about that? If so, what? @RajanMaheshwari – G Buis Apr 13 '17 at 08:44

1 Answers1

4

Okay I figured it out!
https://stackoverflow.com/a/41892828/7385440
This answer lead to the same problem I had. I had to make the identifier different for every notification! So my code now is:

let request = UNNotificationRequest(identifier: bezigheid, content: content, trigger: trigger)

and bezigheid is something that is different in every single cell. Tested it and now I get 2 different notifications!

Community
  • 1
  • 1
G Buis
  • 303
  • 4
  • 17