URB URB - 5 months ago 127
Objective-C Question

Mac OS X NSUserNotificationCenter notification get dismiss event/callback

In our app we are displaying Notification Center notifications in alert style.

Displaying notification works fine, as well as we get callback when user interacts with the notification either by clicking on notification or by clicking on Action button.

However, we are interested in getting a callback or event when user clicks on Other button in notification. I have seen MAC OS does this when it displays its updates available dialog.

Refer to this image for clarification about OS X update available alert:

enter image description here

I have searched this over the internet, as well as gone through Notification Center's documentation this and this as well.

Is there any undocumented API? or some custom mechanism for detecting click on Other (close) button?

Tim Tim

While the other (close) button is clearly meant to dismiss the notification, regardless of what its custom caption may indicate, there is no elegant way to get notified when the user dismisses the notification by clicking on the close button.

What you could do, however, is to monitor the default user notification center's deliveredNotifications property: As long as the notification has not yet been dismissed, the array will contain the notification. Once the notification has been dismissed, the array will not contain it anymore.

This could be implemented in a NSUserNotificationCenter delegate method like this:

- (void)userNotificationCenter:(NSUserNotificationCenter *)center didDeliverNotification:(NSUserNotification *)notification
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0),
                       BOOL notificationStillPresent;
                       do {
                           notificationStillPresent = NO;
                           for (NSUserNotification *nox in [[NSUserNotificationCenter defaultUserNotificationCenter] deliveredNotifications]) {
                               if ([nox.identifier isEqualToString:notification.identifier]) notificationStillPresent = YES;
                           if (notificationStillPresent) [NSThread sleepForTimeInterval:0.20f];
                       } while (notificationStillPresent);
                       dispatch_async(dispatch_get_main_queue(), ^{
                           [self notificationHandlerForNotification:notification];

This code will check if the notification is still there every 200 milliseconds. Once it is gone, the -notificationHandler: method will be called on the main thread, which is just an arbitrary callback method.

In this custom -notificationHandler: method you could check whether NSUserNotificationCenter's didActivateNotification: delegate method has been called for the notification. If it hasn't, the user most likely clicked on the close button of the notification.

This is not failsafe, though, as the user may also have otherwise dismissed the notification, i.e. without clicking on the close button.