D-A UK
D-A UK

Reputation: 1174

Local Notifications Identifiers

I have the following code for the request and posting the notification:

let request = UNNotificationRequest(identifier: "timerDone", content: content, trigger: trigger)

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

It's in a for loop so normally gets executed multiple times - each iteration of the loop results in this code being run, however for each iteration the request has the same identifier.

Does this mean I am overriding the previous notification I just added in the previous iteration of the loop? (the reason I am asking is because only one notification comes through after closing the app even if there are multiple timers; the notification is usually for that of the timer which was the last iteration in the for loop (this code is in application did resign active)).

Upvotes: 0

Views: 2838

Answers (2)

Zdeněk Topič
Zdeněk Topič

Reputation: 780

As stated in Apple documentation:

If you use the same identifier when scheduling a new notification, the system removes the previously scheduled notification with that identifier and replaces it with the new one.

You can always add something unique to your identifier, a simple iteration index/number, UUID, ID of an item or anything else.

let identifier = "timerDone-\(index)"

Upvotes: 3

Dheeraj D
Dheeraj D

Reputation: 4451

Yes, It is overriding you old notification with the new one.

You can make this change Inside your loop:

let strIdentifier = "timerDone_\(i)"
let request = UNNotificationRequest(identifier: strIdentifier, content: content, trigger: trigger)

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

Upvotes: 3

Related Questions