0

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)).

D-A UK
  • 1,084
  • 2
  • 11
  • 24

2 Answers2

3

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)"
Zdeněk Topič
  • 762
  • 4
  • 29
3

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)
Dheeraj D
  • 4,386
  • 4
  • 20
  • 34