Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple notifications in Xcode, Swift 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

like image 709
G Buis Avatar asked Dec 07 '25 08:12

G Buis


1 Answers

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!

like image 96
G Buis Avatar answered Dec 08 '25 22:12

G Buis