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?

shim
  • 9,289
  • 12
  • 69
  • 108
Ryan Pierce
  • 1,583
  • 16
  • 24
  • 1
    you should never use the same identifier twice. – Leo Dabus Sep 27 '17 at 01: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. – Ryan Pierce Sep 27 '17 at 02:09
  • make sure you cancel previous scheduled notifications – Leo Dabus Sep 27 '17 at 02:10
  • https://stackoverflow.com/questions/42882832/trigger-notification-weekly-swift-3/42892780?s=1|3.1375#42892780 – Leo Dabus Sep 27 '17 at 02:11
  • Not sure how that^ link answers my question regarding why using the same identifier twice still works. – Ryan Pierce Sep 27 '17 at 02:18
  • I just wanted you to read the comment suggesting the use of the trigger date description as your notification identifier – Leo Dabus Sep 27 '17 at 02:24

1 Answers1

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.
kulich
  • 161
  • 1
  • 6