'UNNotificationRequest requires unique identifier, but fires on duplicates too

I call local Notifications like so

let center = UNUserNotificationCenter.current()
let content = UNMutableNotificationContent()
content.title = title
content.body = text
content.categoryIdentifier = category
content.userInfo = map
content.sound = UNNotificationSound.default()
content.setValue("YES", forKeyPath: "shouldAlwaysAlertWhileAppIsForeground")
let request = UNNotificationRequest(identifier: "testing", content: content, trigger: nil)
center.add(request)

with the same UNNotificationRequest identifier each time (a non-changing string). According to the docs:

identifier

A unique identifier for the request (if identifier is not unique, a new notification request object is not created). You can use this identifier later to cancel a request that is still pending. This parameter must not be nil.

The local notification fires every time I trigger it, even in the same instance of the app. The identifier is always the same. Are the docs wrong?



Solution 1:[1]

At this point the documentation is corrected and more accurate: https://developer.apple.com/documentation/usernotifications/unnotificationrequest/1649633-init

The system uses the identifier parameter to determine how to handle the request:

  • If you provide a unique identifier, the system creates a new notification.
  • If the identifier matches a previously delivered notification, the system alerts the user again, replaces the old notification with the new one, and places the new notification at the top of the list.
  • If the identifier matches a pending request, the new request replaces the pending request.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 kulich