gpt4 book ai didi

objective-c - didActivateNotification 没有响应 NSUserNotification

转载 作者:行者123 更新时间:2023-12-03 16:51:20 28 4
gpt4 key购买 nike

我正在努力让 osx native 通知正常工作。我可以创建通知,但无法使 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");

有什么建议吗?谢谢

最佳答案

您可能必须为 NSUserNotificationCenter 设置委托(delegate):

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

您还应该确保您的 Notice 类实现 NSUserNotificationCenterDelegate 协议(protocol)。

关于objective-c - didActivateNotification 没有响应 NSUserNotification,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13204746/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com