David David - 1 year ago 131
Objective-C Question

ObjectiveC - how to use NSUserNotification identifier property

According to the apple docs https://developer.apple.com/reference/foundation/nsusernotification/1416410-identifier

NSUserNotification has a property called identifier which is suppose to replace notifications when the identifier is the same as another notification.

When I was testing this feature out however it seems that the notification is not really replaced, just not sent.

How would I achieve the effect where only one type of notification is present in the notification center but the latest one called is updated to the top?

Sending notification A + notification B + notification A with a 1 minute delay

This is what is being shown on the mac notification center

Without identifier

NotificationA (now)
NotificationB (1 minute ago)
NotificationA (2 minute ago)

With identifier

NotificationB (1 minute ago)
NotificationA (2 minute ago)

Notice how Notification A (2nd time) is not called due to the identifier being present

Desired effect

NotificationA (now)
NotificationB (1 minute ago)

In this situation Notification A is sent again and the previous Notification A is gone

Answer Source

You can delete an existing notification using the NSNotificationCenters removeDeliveredNotification: method. Just remove and re-add your notification.


[[NSUserNotificationCenter defaultUserNotificationCenter] removeDeliveredNotification:userNotification];
[[NSUserNotificationCenter defaultUserNotificationCenter] deliverNotification:userNotification];  



I successfully used this technique to show notifications without polluting the notification center.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download