NSUserNotification关闭调用didActivateNotification

时间:2017-10-06 16:43:05

标签: objective-c nsusernotification nsusernotificationcenter

自从MacOS 10.13每次我点击NSUserNotification上的关闭按钮时,它都会调用:

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

如何阻止此操作或处理closeaction按钮

要创建通知,我会这样做:

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

NSUserNotificationAlertStyle中的.plist设置为“alert

但现在基本上关闭按钮的反应方式与actionButton的反应相同吗?

2 个答案:

答案 0 :(得分:2)

NSUserNotification具有可以管理通知标识符或hasActionButton值的属性,因此您可以使用if else在同一委托方法中处理关闭与操作按钮

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

答案 1 :(得分:1)

这对我有用..

您可以在didActivateNotification:方法

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

其中center是......

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