58

Can someone give an example of sending a test notification from a Cocoa app to Notifications Center? eg. when I click on an NSButton

lukas
  • 2,300
  • 6
  • 28
  • 41
haseo98
  • 837
  • 2
  • 10
  • 13

2 Answers2

153

Notifications in Mountain Lion are handled by two classes. NSUserNotification and NSUserNotificationCenter. NSUserNotification is your actual notification, it has a title, a message etc. that can be set via properties. To deliver a notification that you've created, you can use the deliverNotification: method available in NSUserNotificationCenter. The Apple docs have detailed information on NSUserNotification & NSUserNotificationCenter but the basic code to post a notification looks like this:

- (IBAction)showNotification:(id)sender{
    NSUserNotification *notification = [[NSUserNotification alloc] init];
    notification.title = @"Hello, World!";
    notification.informativeText = @"A notification";
    notification.soundName = NSUserNotificationDefaultSoundName;

    [[NSUserNotificationCenter defaultUserNotificationCenter] deliverNotification:notification];
    [notification release];
}

That'll produce a notification with a title, a message and that'll play the default sound when it's displayed. There's a lot more that you can do with notifications than just this (such as scheduling notifications) and that's all detailed in the documentation I linked to.

One small point, notifications will only be displayed when your application is the key application. If you want your notifications to display regardless of if your application is key or not, you'll need to specify a delegate for NSUserNotificationCenter and override the delegate method userNotificationCenter:shouldPresentNotification: so that it returns YES. The documentation for NSUserNotificationCenterDelegate can be found here

Here's an example of providing a delegate to NSUserNotificationCenter and then forcing notifications to be displayed regardless of if your application is key. In your application's AppDelegate.m file, edit it like this:

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
    [[NSUserNotificationCenter defaultUserNotificationCenter] setDelegate:self];
}

- (BOOL)userNotificationCenter:(NSUserNotificationCenter *)center shouldPresentNotification:(NSUserNotification *)notification{
    return YES;
}

And in AppDelegate.h, declare that the class conforms to the NSUserNotificationCenterDelegate protocol:

@interface AppDelegate : NSObject <NSApplicationDelegate, NSUserNotificationCenterDelegate>
Andreas
  • 9,245
  • 9
  • 49
  • 97
alexjohnj
  • 1,758
  • 1
  • 13
  • 12
  • Can you elaborate on how to override userNotificationCenter? (sorry im really new to this :) ) – haseo98 Aug 05 '12 at 12:50
  • 3
    @haseo98 Yup, I've just added an example to my answer. – alexjohnj Aug 05 '12 at 13:10
  • Im getting an error next to the applicationdidfinishlaunching section of the method, Sending 'AppDelegate *const __strong' to parameter of incompatible type 'id'. Any ideas? – haseo98 Aug 05 '12 at 13:25
  • 4
    @haseo98 Whoops, forgot to mention that you need to declare that your AppDelegate conforms to the NSUserNotificationCenterDelegate protocol. Check the updated answer. – alexjohnj Aug 05 '12 at 13:44
  • 1
    Thanks for your help, I was really stuck on how to make it display a notification while the app is in the foreground and this explained it. Still not sure if I want to use that functionality in my app but at least I know how now! – Jackson Aug 07 '12 at 05:45
  • What parts of this are different for iOS? – Cin316 Apr 04 '13 at 16:46
  • @alexjohnj I know this is an old answer, but can you elaborate in which scenarios application won't be a key application? Like if it were only a background application it wouldn't be key? – Markus Aug 04 '15 at 09:40
0

@alexjohnj's answer updated for Swift 5.2

In AppDelegate

func applicationDidFinishLaunching(_ aNotification: Notification) {

  // Set delegate
  NSUserNotificationCenter.default.delegate = self
}

And then confirm to NSUserNotificationCenterDelegate as

extension AppDelegate: NSUserNotificationCenterDelegate {
 func userNotificationCenter(_ center: NSUserNotificationCenter, shouldPresent notification: NSUserNotification) -> Bool {
    true
}}
Parion
  • 428
  • 9
  • 18