Search code examples
cocoanotificationsnsusernotificationnsusernotificationcenter

Cocoa: User Notifications never being delivered


I'm trying to deliver a local notification, it looks something like this:

NSUserNotification *notification = [[NSUserNotification alloc] init];
//set title, subtitle, and sound
[[NSUserNotificationCenter defaultUserNotificationCenter] deliverNotification:notification];

But the notification doesn't appear. I know sometimes notifications are not presented if the app is frontmost but at the time of delivery it's not. In system preferences I made sure that notifications from my app are allowed, and I even overrode the userNotificationCenter:shouldPresentNotification: method to always return YES but it still doesn't present the notification.

What is most confusing is that everything was working fine until I updated to Mavericks. I suppose something has changed in the update but I can't figure out what.

Thanks for the help.


Solution

  • my guess is that something is nil. make sure you are either assigning a (valid and non-nil) title, or informativeText.

    I imagine that having invalid values for other properties such as otherButtonTitle might prevent the notification from being displayed as well.

    Are there any error messages in your console?

    Use the debugger or NSLog() statements to assess the values assigned to the notification. You could also see this problem if the NSUserNotification pointer is nil (not the case from the code you posted, but worth mentioning).

    Here is a minimal test for an app delegate that works on Mac OS X 10.9 (Mavericks):

    - (void)applicationDidFinishLaunching:(NSNotification *)aNotification
    {
        NSUserNotificationCenter* unc = [NSUserNotificationCenter defaultUserNotificationCenter];
        unc.delegate = self;
    
        NSUserNotification* notice = [[NSUserNotification alloc] init];
        notice.title = @"title"; // notifications need a title, or informativeText at minimum to appear on screen
        //notice.informativeText = @"informativeText";
    
        NSLog(@"notification: %@, notification center:%@", notice, unc);
        [unc deliverNotification:notice];
    }
    
    // The notifications will always dispaly even if we are in the foreground
    - (BOOL)userNotificationCenter:(NSUserNotificationCenter *)center shouldPresentNotification:(NSUserNotification *)notification
    {
        return YES;
    }