didActivateNotification没有响应NSUserNotification

AnA*_*ice 1 macos cocoa objective-c ios

我正在努力让osx原生通知正常工作.我能够创建通知但不能使didActivateNotification正常工作.我希望didActivateNotification允许我关注窗口并从通知中心删除通知.

这是我的代码: notice.m

#import "Notice.h"

@implementation Notice

- (void) notify:(NSDictionary *)message {

    NSLog(@"Notification - Show it");

    NSUserNotification *notification = [[NSUserNotification alloc] init];
    [notification setTitle:[message valueForKey:@"title"]];
    [notification setInformativeText:[message valueForKey:@"content"]];
    [notification setDeliveryDate:[NSDate dateWithTimeInterval:0 sinceDate:[NSDate date]]];
    [notification setSoundName:NSUserNotificationDefaultSoundName];
    NSUserNotificationCenter *center = [NSUserNotificationCenter defaultUserNotificationCenter];
    [center scheduleNotification:notification];
}

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

    NSLog(@"Notification - Clicked");

    notification=nil;
    [center removeDeliveredNotification: notification];
}
Run Code Online (Sandbox Code Playgroud)

这是正确的射击:

NSLog(@"Notification - Show it");
Run Code Online (Sandbox Code Playgroud)

但这不是:

NSLog(@"Notification - Clicked");
Run Code Online (Sandbox Code Playgroud)

有什么建议?谢谢

Nat*_*usa 6

您可能必须设置委托NSUserNotificationCenter:

NSUserNotificationCenter *center = [NSUserNotificationCenter defaultUserNotificationCenter];
center.delegate = self;
Run Code Online (Sandbox Code Playgroud)

您还应该确保您的Notice类实现NSUserNotificationCenterDelegate协议.