User Notifications work with your apps to let you know when something has changed. For example, you can see when there’s upcoming appointments in Calendar or incoming email in Mail. Each app displays these notifications as a banner, alert, or badge.
How to display User Notification as small popup message?
How to display a User Notification in the Notification Center?
Displaying such a user notification is quite easy. Foundation Framework provides NSUserNotification and NSUserNotificationCenter class. The following code shows a usage example:
1 2 3 4 5 | NSUserNotification *notification = [[NSUserNotification alloc] init]; notification.title = @"Hello, World!"; notification.informativeText = [NSString stringWithFormat:@"Life is unbelievable"]; notification.soundName = NSUserNotificationDefaultSoundName; [[NSUserNotificationCenter defaultUserNotificationCenter] deliverNotification:notification]; |
This code should display a notification in the Notification Center. But it’s not displayed by default as a popup message. To display it we must to implement NSUserNotificationCenterDelegate protocol. And the method we need is called userNotificationCenter:shouldPresentNotification:. Here is an example.
1 2 3 4 5 | - (BOOL)userNotificationCenter:(NSUserNotificationCenter *)center shouldPresentNotification:(NSUserNotification *)notification { return YES; } |
It’s quite easy.
And finally we need to set a class a delegate of the NSUserNotificationCenter as self. A good method for that is applicationDidFinishLaunching: in AppDelegate class:
1 2 3 4 5 | - (void)applicationDidFinishLaunching:(NSNotification *)aNotification { NSUserNotificationCenter *userNotificationCenter = [NSUserNotificationCenter defaultUserNotificationCenter]; userNotificationCenter.delegate = self; } |
1 2 3 4 5 | // Set to NO if the notification has no action button. This will be the case for notifications that are purely for informational purposes and have no user action. The default value is YES. @property BOOL hasActionButton; // The title of the button displayed in the notification. Must be localized as it will be presented to the user. String will be truncated to a length appropriate for display. @property (copy) NSString *actionButtonTitle; |
1 2 3 4 5 6 7 8 | // Specifies when (in an absolute time) the notification should be delivered. After a notification is delivered, it may be presented to the user. @property (nullable, copy) NSDate *deliveryDate; // Set the time zone to interpret the delivery date in. If this value is nil and the user switches time zones, the notification center will adjust the time of presentation to account for the time zone change. If a notification should be delivered at a time in a specific time zone (regardless if the user switches time zones), set this value to that time zone. One common value may be the current time zone. @property (nullable, copy) NSTimeZone *deliveryTimeZone; // The date components that specify how a notification is to be repeated. This value may be nil if the notification should not repeat. The date component values are relative to the date the notification was delivered. If the calendar value of the deliveryRepeatInterval is nil, the current calendar will be used to calculate the repeat interval. For example, if a notification should repeat every hour, set the 'hour' property of the deliveryRepeatInterval to 1. @property (nullable, copy) NSDateComponents *deliveryRepeatInterval; |