3

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?

6
  • 1
    you should never use the same identifier twice.
    – Leo Dabus
    Sep 27, 2017 at 1:14
  • I understand that its bad practice, and I don't in production code. But during testing, what I saw contradicted the documentation since they say 'object is not created,' yet a new notification was sent on the same identifier. Sep 27, 2017 at 2:09
  • make sure you cancel previous scheduled notifications
    – Leo Dabus
    Sep 27, 2017 at 2:10
  • Not sure how that^ link answers my question regarding why using the same identifier twice still works. Sep 27, 2017 at 2:18

1 Answer 1

2

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.