NSUserNotification close calls didActivateNotification

440 Views Asked by At

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??

2

There are 2 best solutions below

2
On BEST ANSWER

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{    
}
0
On

This is works for me..

you can remove notification in didActivateNotification: method

- (void) userNotificationCenter:(NSUserNotificationCenter *)center didActivateNotification:(NSUserNotification *)notification
{
    NSLog(@"Notification - Clicked");
    [center removeDeliveredNotification: notification];
    notification=nil;
}

where center is...

NSUserNotificationCenter *center = [NSUserNotificationCenter defaultUserNotificationCenter];
    [center scheduleNotification:notification];