I am using NSUserNotificationCenter to display scheduled Notifications. I have notifications of my app in the right side notification panel so whenever I click on the notification it'll launch the app but it won't remove notification from the panel.
- When app is not running and I clicked on the notification, applicationDidFinishLaunching is called and it won't remove notification because didActivateNotification delegate is not called.
- When application is already running and I clicked on the notification, appShouldHandleReopen is called. And won't call the delegate.
-(void)userNotificationCenter:(id)center didActivateNotification:(id)notification
I checked behavior in all the standard app they will remove notification when user click on the notifications.
I have implemented it as follows:
/* 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
However this is not working in my case. Please let me know some pointer on it...
Other thing I observed:
- When I implement NSUserNotificationDelegate into AppDelegate it'll work for 2nd case i.e. (When application is already running and I clicked on the notification) - in this case notification is removed.
And for deleting notification for 1st case i.e. (When app is not running and I clicked on the notification, appDidFinishLaunching is called) I wrote following code in appDidFinishLaunching.
NSUserNotification *userNoti = [[notification userInfo] valueForKey:@"NSApplicationLaunchUserNotificationKey"]; [[NSUserNotificationCenter defaultUserNotificationCenter] removeDeliveredNotification:userNoti];
- Can anybody let me know whats the reason, why this works in the AppDelegate but not in my above implementation or Am I doing something wrong in first one?
- Is my AppDelegate implementation fine to handle my both scenarios?
Thanks in Advance.