【发布时间】:2012-10-22 14:04:00
【问题描述】:
我正在使用 NSUserNotification 来显示通知。这工作正常。问题是当你点击通知时:
- 应用通知不会从通知中心移除。
- 应用(最小化时)打不开。
任何熟悉 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