Native Notifications on OSX 10.8+
macOS ObjectiveCSince the introduction of Mountain Lion (OSX 10.8), Notification Center and Notifications are possible for native applications on OSX just like on iOS.
A NSUserNotification can be easily created as follows:
NSUserNotification *notification = [[NSUserNotification alloc] init];
[notification setTitle:@"Title"];
[notification setSubtitle:@"Subtitle"];
[notification setInformativeText:@"Informative Text"];
[notification setDeliveryDate:[NSDate dateWithTimeInterval:0
sinceDate:[NSDate date]]];
[notification setSoundName:NSUserNotificationDefaultSoundName];
and then sent to the NSUserNotificationCenter using
[[NSUserNotificationCenter defaultUserNotificationCenter] scheduleNotification:notification];
In this case our notification is scheduled for delivery - if we want to explicitly deliver it ourselves, simply use the deliverNotification: method. We can even removeScheduledNotification:, removeDeliveredNotification:, or removeAllDeliveredNotifications.
As you’d expect, you can get callbacks on these various methods by implementing the NSUserNotificationCenterDelegate
- (void) userNotificationCenter:(NSUserNotificationCenter *)center
didActivateNotification:(NSUserNotification *)notification
{
}
- (void) userNotificationCenter:(NSUserNotificationCenter *)center
didDeliverNotification:(NSUserNotification *)notification
{
}
//Sent to the delegate when the user notification center has decided not to present your notification.
- (BOOL) userNotificationCenter:(NSUserNotificationCenter *)center
shouldPresentNotification:(NSUserNotification *)notification
{
return YES;
}
userNotificationCenter:shouldPresentNotification: can be especially useful as you can explicitly force the display of a notification onscreen (which can be suppressed by the Notification Center if, for instance, the application is already in the foreground), while userNotificationCenter:didActivateNotification: is called after a user clicked on the notification.
If we wish to incorporate buttons into this notification (so that the user has the option of easily choosing between different options from the mere notification), we first need to make a plist entry for NSUserNotificationAlertStyle to alert, and then augment our code as follows
[notification setHasActionButton: YES];
[notification setActionButtonTitle: @"Action Button"];
[notification setOtherButtonTitle: @"Other Button"];
Note that regardless of these delegates and methods, the Notification Center WILL suppress notifications if the user has opted out of them in System Preferences, or repurpose ALERT notifications displayed as mere BANNERs etc. if the user has chosen so.