The editable content for a notification.
SDKs
- iOS 10.0+
- macOS 10.14+
- Mac Catalyst 13.0+
- tvOS 10.0+
- watchOS 3.0+
Framework
- User
Notifications
Declaration
@interface UNMutableNotificationContent : UNNotification Content
Overview
Create a UNMutable
object when you want to specify the payload for a local notification. Specifically, use this object to specify the title and message for an alert, the sound to play, or the value to assign to your app's badge. You might also provide details about how the system handles the notification. For example, you can specify a custom launch image and a thread identifier for visually grouping related notifications.
After creating your content object, assign it to a UNNotification
object, add a trigger condition, and schedule your notification. The trigger condition defines when the notification is delivered to the user. Listing 1 shows the scheduling of a local notification that displays an alert and plays a sound after a delay of five seconds. The strings for the alert's title and body are stored in the app’s Localizable
file.
Creating the content for a local notification
// Configure the notification's payload.
UNMutableNotificationContent* content = [[UNMutableNotificationContent alloc] init];
content.title = [NSString localizedUserNotificationStringForKey:@"Hello!" arguments:nil];
content.body = [NSString localizedUserNotificationStringForKey:@"Hello_message_body"
arguments:nil];
content.sound = [UNNotificationSound defaultSound];
// Deliver the notification in five seconds.
UNTimeIntervalNotificationTrigger* trigger = [UNTimeIntervalNotificationTrigger
triggerWithTimeInterval:5 repeats:NO];
UNNotificationRequest* request = [UNNotificationRequest requestWithIdentifier:@"FiveSecond"
content:content trigger:trigger];
// Schedule the notification.
UNUserNotificationCenter* center = [UNUserNotificationCenter currentNotificationCenter];
[center addNotificationRequest:request];
Note
Local notifications always result in user interactions, and the system ignores any interactions for which your app is not authorized. For information about how to request authorization for user interactions, see Asking Permission to Use Notifications.
Localizing the Alert Strings
The strings you display in a notification alert should be localized for the current user. Although you can use the NSLocalized
macros to load strings from your app’s resource files, a better option is to specify your string using the localized
method of NSString
. The localized
method delays the loading of the localized string until the notification is delivered. Thus, if the user changes language settings before a notification is delivered, the alert text is updated to the user’s current language instead of the language that was set when the notification was scheduled.