The editable content for a notification.


A UNMutableNotificationContent object contains the data associated with a notification. You use the properties of this class to specify the parameters of a local notification, such as whether the notification adds a badge to the app’s icon, plays a notification sound, or displays an alert. You can also provide app-specific identifiers to help the system determine how to process the notification.

You use this class by creating an instance of it, configuring its properties, and assigning it to a UNNotificationRequest object. Configuring a request object in this way creates a local notification for you to schedule. The trigger condition associated with the request object defines when the notification is delivered to the user. Listing 1 shows the creation of a content object whose notification displays an alert and plays a sound after a delay of five seconds. The strings for the alert are stored in the app’s Localizable.strings file using the specified keys and loaded just prior to displaying the alert.

Listing 1

Creating the content for a local notification

let content = UNMutableNotificationContent()
content.title = NSString.localizedUserNotificationStringForKey("Hello!", arguments: nil)
content.body = NSString.localizedUserNotificationStringForKey("Hello_message_body", arguments: nil)
content.sound = UNNotificationSound.default() 
// Deliver the notification in five seconds.
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 5, repeats: false)
let request = UNNotificationRequest(identifier: "FiveSecond", content: content, trigger: trigger) // Schedule the notification.
let center = UNUserNotificationCenter.current()
center.add(request) { (error : Error?) in
     if let theError = error {
         // Handle any errors

Localizing the Alert Strings

The strings you display in a notification alert should be localized for the current user. Although you can use the NSLocalizedString macros to load strings from your app’s resource files, a better option is to use the localizedUserNotificationString(forKey:arguments:) method to specify the string. The localizedUserNotificationString(forKey:arguments:) 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.


Getting the Notification Data

var title: String

A short description of the reason for the alert.

var subtitle: String

A secondary description of the reason for the alert.

var body: String

The message displayed in the notification alert.

var badge: NSNumber?

The number to apply to the app’s icon.

var sound: UNNotificationSound?

The sound to play when the notification is delivered.

var launchImageName: String

The name of the launch image to display when your app is launched in response to the notification

var userInfo: [AnyHashable : Any]

A dictionary of custom information associated with the notification.

var attachments: [UNNotificationAttachment]

An array of attachments to display with the notification.

Identifying the Content

var categoryIdentifier: String

The identifier of the app-defined category object.

var threadIdentifier: String

An app-specific identifier that you attach to related notifications.


Conforms To

See Also

Notification Content

class UNNotificationContent

The uneditable content of a local or remote notification.

class UNNotificationAttachment

A media file associated with a notification.

class UNNotificationSound

A sound to be played when a notification is delivered.