10

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

ありがとうございました

4

1 に答える 1

12

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

通知がアクティブ化されたとき(ユーザーがクリック)、パネルでユーザーに通知するだけです(ハッドウィンドウを使用できます)。この場合、配信された通知をすぐに削除しますが、これは通常は発生しません。通知は残る可能性があります。しばらく時間がかかり、30分後に削除されます(開発しているアプリケーションによって異なります)。

于 2012-11-01T22:52:14.667 に答える