NSUserNotification - How open the app when clicked
Asked Answered
C

1

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

Cooks answered 1/11, 2012 at 22:12 Comment(1)
[[NSUserNotificationCenter defaultUserNotificationCenter] setDelegate:self];Headway
M
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).

Marashio answered 1/11, 2012 at 22:52 Comment(7)
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.Pegmatite
Would you mind updating the above so I could learn? I'm new and trying to ramp up. Really appreciate it.Cooks
Also, is this for removing notifications or for opening the app up from a minimized hidden view? I suppose I asked two questions :)Cooks
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: .Marashio
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? ThanksCooks
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.Marashio
@ColdTree: Set the center's delegate to yourself.Pegmatite

© 2022 - 2024 — McMap. All rights reserved.