Objective c 如何删除NSUserNotification
我正在使用NSUserNotificationCenter显示计划通知。我在右侧的通知面板中有我的应用程序的通知,因此每当我单击通知时,它都会启动应用程序,但不会从面板中删除通知Objective c 如何删除NSUserNotification,objective-c,cocoa,nsusernotificationcenter,Objective C,Cocoa,Nsusernotificationcenter,我正在使用NSUserNotificationCenter显示计划通知。我在右侧的通知面板中有我的应用程序的通知,因此每当我单击通知时,它都会启动应用程序,但不会从面板中删除通知 当应用程序未运行且我单击了通知时,将调用ApplicationIDFinishLaunching,并且不会删除通知,因为未调用didActivateNotification委托 当应用程序已经运行并且我单击了通知时,将调用appShouldHandleReopen。不会给代表打电话 -(无效)用户通知中心:(id)中心
/* will create notification and set the delegate*/
@interface NotificationViewController : NSViewController
{
id delegate;
}
-(void)showNotification;
@end
@implementation NotificationViewController
- (void)createNotification:(NSString*)str
{
Class UserNotificationClass=NSClassFromString(@"NSUserNotification");
id notification = [[UserNotificationClass alloc] init];
//Set the title of the notification
[notification setTitle:@"My Notification"];
[notification setSubtitle:@"Test"];
[notification setHasActionButton:YES];
[notification setActionButtonTitle:@"OK"];
[notification setOtherButtonTitle:@"CANCEL"];
//Set the text of the notification
[notification setInformativeText:str];
[notification setDeliveryDate:[NSDate dateWithTimeInterval:0.1 sinceDate:[NSDate date]]];
[notification setSoundName:@"NSUserNotificationDefaultSoundName"];
[notification setUserInfo:[NSDictionary dictionaryWithObjectsAndKeys:str,@"string",nil]];
Class UserNotificationCenterClass=NSClassFromString(@"NSUserNotificationCenter");
id center = [UserNotificationCenterClass defaultUserNotificationCenter];
[center setDelegate:self.delegate];
//Scheldule our NSUserNotification
[center scheduleNotification:notification];
[notification release];
}
@end
/* Class which implements NSUserNotificationDelegate*/
@interface NotificationHandler : NSObject <NSUserNotificationCenterDelegate>
{
}
- (void)createNotification:(NSString*)str;
@end
@implementation NotificationHandler
- (void)createNotification:(NSString*)str
{
NotificationHandler *notificationHandler = [[NotificationHandler alloc] initWithNibName:@"NotificationHandler" bundle:[NSBundle mainBundle]];
notificationHandler.delegate = self;
[notificationHandler showNotification];
}
- (void)userNotificationCenter:(id)center didActivateNotification:(id)notification
{
[center removeDeliveredNotification:notification];
}
- (BOOL)userNotificationCenter:(id)center shouldPresentNotification:(id)notification
{
return YES;
}
@end
/*将创建通知并设置代理*/
@接口通知ViewController:NSViewController
{
id代表;
}
-(无效)通知;
@结束
@实现通知视图控制器
-(void)createNotification:(NSString*)str
{
类UserNotificationClass=NSClassFromString(@“NSUserNotification”);
id通知=[[UserNotificationClass alloc]init];
//设置通知的标题
[通知集标题:@“我的通知”];
[通知集标题:@“测试”];
[通知setHasActionButton:是];
[通知设置操作按钮:@“确定”];
[通知设置其他按钮:@“取消”];
//设置通知的文本
[通知setInformativeText:str];
[notification setDeliveryDate:[NSDate date WithTime Interval:0.1 sinceDate:[NSDate date]];
[通知setSoundName:@“NSUserNotificationDefaultSoundName”];
[notification setUserInfo:[NSDictionary Dictionary WithObjectsSandKeys:str,@“string”,nil];
类UserNotificationCenterClass=NSClassFromString(@“NSUserNotificationCenter”);
id中心=[UserNotificationCenterClass defaultUserNotificationCenter];
[center setDelegate:self.delegate];
//安排我们的通知
[中心计划通知:通知];
[通知发布];
}
@结束
/*实现NSUserNotificationDelegate的类*/
@接口NotificationHandler:NSObject
{
}
-(void)createNotification:(NSString*)str;
@结束
@实现NotificationHandler
-(void)createNotification:(NSString*)str
{
NotificationHandler*NotificationHandler=[[NotificationHandler alloc]initWithNibName:@“NotificationHandler”bundle:[NSBundle mainBundle]];
notificationHandler.delegate=self;
[通知处理程序显示通知];
}
-(无效)用户通知中心:(id)中心指示激活通知:(id)通知
{
[中心移除传递通知:通知];
}
-(BOOL)用户通知中心:(id)中心应发送通知:(id)通知
{
返回YES;
}
@结束
然而,这在我的情况下不起作用。请让我知道一些关于它的指示
我观察到的另一件事:
- 当我将NSUserNotificationDelegate实现到AppDelegate中时,它将适用于第二种情况,即(当应用程序已经运行并且我单击了通知时)-在这种情况下,通知将被删除
- 为了删除第一种情况下的通知,即(当应用程序未运行且我单击通知时,调用appDidFinishLaunching),我在appDidFinishLaunching中编写了以下代码
NSUserNotification*userNoti=[[notification userInfo]valueForKey:@“NSApplicationLaunchUserNotificationKey”];
[[NSUserNotificationCenter defaultUserNotificationCenter]removeDeliveredNotification:userNoti]
- 有人能告诉我原因是什么,为什么这在AppDelegate中有效,但在我上面的实现中不起作用,或者我在第一个实现中做错了什么
- 我的AppDelegate实现可以处理这两种情况吗李>