1

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

David
  • 87
  • 1
  • 11
  • 1
    We faced a similar problem in Chrome and we ended up deleting the notification and re-adding it again. The code is not yet enabled by default but you can try it out by toggling some flags. Happy to provide more info if that helps. https://cs.chromium.org/chromium/src/chrome/browser/notifications/notification_platform_bridge_mac.mm?rcl=0&l=142 – Miguel Garcia Jul 20 '16 at 09:09
  • That does, thank you – David Jul 20 '16 at 18:46

1 Answers1

2

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

Objective-C

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

Swift

NSUserNotificationCenter.default.removeDeliveredNotification(userNotification)
NSUserNotificationCenter.default.deliver(userNotification)

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

mangerlahn
  • 4,746
  • 2
  • 26
  • 50
  • 1
    Thanks for the answer, but the order should be reversed: first removing previous notification and then delivering new. – Alek Nov 15 '16 at 09:50
  • You're right, in this example we should first remove the old one. I used this code where I wanted to show a notification, but didn't want it to appear in Notification Center – mangerlahn Nov 15 '16 at 12:27