首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >NSUserNotification -点击时如何打开应用程序

NSUserNotification -点击时如何打开应用程序
EN

Stack Overflow用户
提问于 2012-11-01 22:12:23
回答 1查看 7.5K关注 0票数 10

我使用NSUserNotification来显示通知。这很好用。问题是当您单击通知时:

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

熟悉NSUserNotification的人谁能提供一些指点呢?

注意事项m

代码语言:javascript
复制
#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

谢谢

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-11-01 22:52:14

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

代码语言:javascript
复制
- (void)userNotificationCenter:(NSUserNotificationCenter *)center didActivateNotification:(NSUserNotification *)notification

示例:

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

代码语言:javascript
复制
- (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窗口).In--在这种情况下,我会立即删除交付的通知,但这不是发生的情况,usually.The通知可能会在那里停留一段时间并在1/2小时后被删除(这取决于您正在开发的应用程序)。

票数 12
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13186320

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档