【问题标题】:NSUserNotification - How open the app when clickedNSUserNotification - 单击时如何打开应用程序
【发布时间】:2012-10-22 14:04:00
【问题描述】:

我正在使用 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

谢谢

【问题讨论】:

  • [[NSUserNotificationCenter defaultUserNotificationCenter] setDelegate:self];

标签: objective-c macos cocoa nsusernotification


【解决方案1】:

只需实现 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:将中心的代表设置为您自己。
猜你喜欢
  • 1970-01-01
  • 2012-08-15
  • 1970-01-01
  • 2014-05-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多