gpt4 book ai didi

objective-c - NSUserNotification - 单击时如何打开应用程序

转载 作者:太空狗 更新时间:2023-10-30 03:26:18 26 4
gpt4 key购买 nike

我正在使用 NSUserNotification 来显示通知。这工作正常。问题是当您点击通知时:

  1. 应用通知不会从通知中心删除。
  2. 应用(最小化时)打不开。

熟悉 NSUserNotification 的人可以提供一些指导吗?

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];
}







#pragma mark WebScripting Protocol

+ (BOOL) isSelectorExcludedFromWebScript:(SEL)selector
{
if (selector == @selector(notify:))
return NO;

return YES;
}

+ (NSString*) webScriptNameForSelector:(SEL)selector
{
id result = nil;

if (selector == @selector(notify:)) {
result = @"notify";
}

return result;
}

// right now exclude all properties (eg keys)
+ (BOOL) isKeyExcludedFromWebScript:(const char*)name
{
return YES;
}

@end

谢谢

最佳答案

只需实现 NSUserNotificationCenterDelegate 并定义此方法:

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

示例:

这是我在“通知程序”应用程序中所做的。

- (void) userNotificationCenter:(NSUserNotificationCenter *)center didActivateNotification:(NSUserNotification *)notification
{
NSRunAlertPanel([notification title], [notification informativeText], @"Ok", nil, nil);
}

- (void) userNotificationCenter:(NSUserNotificationCenter *)center didDeliverNotification:(NSUserNotification *)notification
{
notifications=nil;
[tableView reloadData];
[center removeDeliveredNotification: notification];
}

当通知被激活(用户点击)时,我只是用一个面板通知用户(我可以使用一个平视显示器窗口)。在这种情况下,我会立即删除传递的通知,但通常不会发生这种情况。通知可能会在那里停留一段时间,并在 1/2 小时后被删除(这取决于您正在开发的应用程序)。

关于objective-c - NSUserNotification - 单击时如何打开应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13186320/

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