maha::software

  • Archive
  • RSS

How to use the 10.8 Notification Center API

I found Apple’s SDK documentation on Notification Center in Mountain Lion to be somewhat lacking. There doesn’t seem to be a basic “Hello World” example. So, here you go:

NSUserNotification *notification = [[NSUserNotification alloc] init];
[notification setTitle:@"Hello Panda"];
[notification setInformativeText:@"I love PandaBar!"];
[notification setSoundName:NSUserNotificationDefaultSoundName];

NSUserNotificationCenter *center = [NSUserNotificationCenter defaultUserNotificationCenter];
[center setDelegate:self];
[center deliverNotification:notification];

By default, Notification Center only displays notifications if the application is not frontmost. To always present the notification, you need to set the delegate on the NSUserNotificationCenter as shown above, and implement the NSUserNotificationCenterDelegate as follows:

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

Additionally, the above code will present the notification immediately. If you want to schedule the notification for a later time, you can do something like:

[notification setDeliveryDate:[NSDate dateWithTimeInterval:20 sinceDate:[NSDate date]]];

And then use [NSUserNotificationCenter scheduleNotification:] instead of deliverNotification:

Any other useful Notification Center API tips? Let me know!

  • 9 months ago
  • Comments
  • Permalink
  • Share
    Tweet

Recent comments

Blog comments powered by Disqus
← Previous • Next →

About

Thoughts on software development and life.

Twitter

loading tweets…

  • RSS
  • Random
  • Archive
  • Mobile

Effector Theme by Pixel Union.

Powered by Tumblr