首页 文章

NSUserNotification关闭调用didActivateNotification

提问于
浏览
3

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

并且 .plist 中的 NSUserNotificationAlertStyle 设置为“ alert

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

2 回答

  • 2

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

    - (void) userNotificationCenter:(NSUserNotificationCenter *)center didActivateNotification:(NSUserNotification *)notification{    
    }
    
  • 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];
    

相关问题