10

I'm using NSUserNotification to display notifications. This is working fine. The problem is that when you click on a notification:

  1. The apps notifications are not removed from the notification center.
  2. The app (when minimized) does not open.

Anyone familiar with the NSUserNotification who can offer some pointers?

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

Thank you

AnApprentice
  • 108,152
  • 195
  • 629
  • 1,012

1 Answers1

12

Just implement the NSUserNotificationCenterDelegate and define this method:

- (void)userNotificationCenter:(NSUserNotificationCenter *)center didActivateNotification:(NSUserNotification *)notification

Example:

This is what I did in a "notifier" application.

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

When the notification is activated (click by the user) I just inform the user with a panel (I could use a hud window).In this case I immediately remove the delivered notification, but this is not what happens usually.The notification could stay there some time and be removed after 1/2 hours (it depends on the application that you are developing).

Ramy Al Zuhouri
  • 21,580
  • 26
  • 105
  • 187
  • 5
    Don't forget to actually set yourself as the delegate. Declaring to nobody in particular that you can be a delegate achieves nothing if you never actually become the delegate at any point. – Peter Hosey Nov 01 '12 at 23:35
  • Would you mind updating the above so I could learn? I'm new and trying to ramp up. Really appreciate it. – AnApprentice Nov 01 '12 at 23:48
  • Also, is this for removing notifications or for opening the app up from a minimized hidden view? I suppose I asked two questions :) – AnApprentice Nov 01 '12 at 23:49
  • 1
    If you implement this method, it will get executed when you click on a notification.If you implement it the app will run if it was not running.As for removing a notification so that a user doesn't see it anymore, use NSUserNotificationCenter's instance method removeDeliveredNotification: . – Ramy Al Zuhouri Nov 02 '12 at 00:09
  • Hello, Still can't get this working. I updated my question with the new code provided with the answer. Notice how on notification click, the NSlog for "clicked" is not being logged. Ideas? Thanks – AnApprentice Nov 03 '12 at 00:00
  • The NSLog doesn't work because if you stop the xcode simulation, then the console will be closed.Do something else like for example, launching a NSAlert panel. – Ramy Al Zuhouri Nov 04 '12 at 01:12
  • 1
    @ColdTree: Set the center's delegate to yourself. – Peter Hosey Nov 04 '12 at 17:59