1

我正在努力让 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];
}

这是正确射击:

NSLog(@"Notification - Show it");

但这不是:

NSLog(@"Notification - Clicked");

有什么建议么?谢谢

4

1 回答 1

6

您可能必须将委托设置为NSUserNotificationCenter

NSUserNotificationCenter *center = [NSUserNotificationCenter defaultUserNotificationCenter];
center.delegate = self;

你还应该确保你的Notice类实现了NSUserNotificationCenterDelegate协议。

于 2012-11-03T00:21:33.757 回答