Search code examples
objective-cnsusernotificationnsusernotificationcenter

NSUserNotification close calls didActivateNotification


Since MacOS 10.13 everytime I click the close button on a NSUserNotification it calls:

- (void) userNotificationCenter:(NSUserNotificationCenter *)center didActivateNotification:(NSUserNotification *)notification

How can I prevent this or handle the close vs the action button

To create the notification I do:

NSUserNotification *notification = [[NSUserNotification alloc] init];
...
[notification setHasActionButton:false];
[[NSUserNotificationCenter defaultUserNotificationCenter] deliverNotification:notification];
[[NSUserNotificationCenter defaultUserNotificationCenter] setDelegate:(id)self];

and NSUserNotificationAlertStyle in the .plist is set to "alert"

but now basically the close button reacts the same way the actionButton does??


Solution

  • NSUserNotification has property from which you can manage notification identifier or hasActionButton value, so you can handle the close vs the action button with if else in the same delegate method

    - (void) userNotificationCenter:(NSUserNotificationCenter *)center didActivateNotification:(NSUserNotification *)notification{    
    }