Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Notifications in Mountain Lion are handled by two classes. <code>NSUserNotification</code> and <code>NSUserNotificationCenter</code>. <code>NSUserNotification</code> 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 <code>deliverNotification:</code> method available in NSUserNotificationCenter. The Apple docs have detailed information on <a href="https://developer.apple.com/library/mac/#documentation/Foundation/Reference/NSUserNotification_Class/Reference/Reference.html">NSUserNotification</a> &amp; <a href="https://developer.apple.com/library/mac/#documentation/Foundation/Reference/NSUserNotificationCenter_Class/Reference/Reference.html">NSUserNotificationCenter</a> but the basic code to post a notification looks like this:</p> <pre><code>- (IBAction)showNotification:(id)sender{ NSUserNotification *notification = [[NSUserNotification alloc] init]; notification.title = @"Hello, World!"; notification.informativeText = @"A notification"; notification.soundName = NSUserNotificationDefaultSoundName; [[NSUserNotificationCenter defaultUserNotificationCenter] deliverNotification:notification]; } </code></pre> <p>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. </p> <p>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 <code>NSUserNotificationCenter</code> and override the delegate method <code>userNotificationCenter:shouldPresentNotification:</code> so that it returns YES. The documentation for <code>NSUserNotificationCenterDelegate</code> can be found <a href="https://developer.apple.com/library/mac/#documentation/Foundation/Reference/NSUserNotificationCenterDelegate_Protocol/Reference/Reference.html#//apple_ref/occ/intf/NSUserNotificationCenterDelegate">here</a></p> <p>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:</p> <pre><code>- (void)applicationDidFinishLaunching:(NSNotification *)aNotification { [[NSUserNotificationCenter defaultUserNotificationCenter] setDelegate:self]; } - (BOOL)userNotificationCenter:(NSUserNotificationCenter *)center shouldPresentNotification:(NSUserNotification *)notification{ return YES; } </code></pre> <p>And in AppDelegate.h, declare that the class conforms to the NSUserNotificationCenterDelegate protocol:</p> <pre><code>@interface AppDelegate : NSObject &lt;NSApplicationDelegate, NSUserNotificationCenterDelegate&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload