I’m working to get osx native notifications working. I’m able to create a notification but not get the didActivateNotification working. I want didActivateNotification to allow me to focus the window and remove the notification from the notification center.
Here is my code: 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];
}
This is firing properly:
NSLog(@"Notification - Show it");
But this is not:
NSLog(@"Notification - Clicked");
Any suggestions? Thanks
You probably have to set the delegate for
NSUserNotificationCenter:You should also make sure your
Noticeclass implements theNSUserNotificationCenterDelegateprotocol.