NSUserNotification 关闭调用 didActivateNotification

NSUserNotification close calls didActivateNotification

从 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 的反应方式相同??

这对我有用..

您可以在 didActivateNotification: 方法中删除通知

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

其中 center 是...

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

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

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