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

Objective c NSUserNotification-单击时如何打开应用程序,objective-c,macos,cocoa,nsusernotification,Objective C,Macos,Cocoa,Nsusernotification,我正在使用NSUserNotification显示通知。这很好用。问题在于,单击通知时: 不会从通知中心删除应用程序通知 应用程序(最小化时)未打开 任何熟悉NSUserNotification的人都可以提供一些建议吗 注意.m #import "Notice.h" @implementation Notice - (void) notify:(NSDictionary *)message { NSLog(@"Notification - Show it"); NSUse

我正在使用NSUserNotification显示通知。这很好用。问题在于,单击通知时:

  • 不会从通知中心删除应用程序通知
  • 应用程序(最小化时)未打开
  • 任何熟悉NSUserNotification的人都可以提供一些建议吗

    注意.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];
    }
    

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

    不要忘记将自己实际设置为代表。如果你在任何时候都没有真正成为代表,那么向任何人特别声明你可以成为代表将一事无成。你介意更新上述内容以便我学习吗?我是新来的,正在努力提升自己。非常感谢。另外,这是用于删除通知还是用于从最小化的隐藏视图打开应用程序?我想我问了两个问题:)如果你实现了这个方法,它会在你点击一个通知时被执行。如果你实现了它,应用程序会在它没有运行的情况下运行。至于删除一个通知以便用户不再看到它,使用NSUserNotificationCenter的实例方法removeDeliveredNotification:。@ColdTree:将中心的委托设置为您自己。[[NSUserNotificationCenter defaultUserNotificationCenter]setDelegate:self];