简体   繁体   中英

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

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

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];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM