Class

UNMutableNotificationContent

The editable content for a notification.

Declaration

class UNMutableNotificationContent : UNNotificationContent

Overview

Create a UNMutableNotificationContent 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 UNNotificationRequest 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.strings file.

Listing 1

Creating the content for a local notification

// Configure the notification's payload.
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 specify your string using the localizedUserNotificationString(forKey:arguments:) method of NSString. 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.

Topics

Getting the Notification Data

var title: String

The localized title, containing the reason for the alert.

var subtitle: String

The localized subtitle, containing a secondary description of the reason for the alert.

var body: String

The localized message to display 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 custom 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 in an alert-based notification.

Setting the Notification Summary

var summaryArgument: String

The string the notification adds to the category’s summary format string.

var summaryArgumentCount: Int

The number of items the notification adds to the category’s summary format string.

Identifying the Content

var categoryIdentifier: String

The identifier of the category object that represents the notification's type.

var threadIdentifier: String

An identifier that you use to group related notifications together.

Relationships

Conforms To

See Also

Notification Content

class UNNotificationContent

The uneditable content of a notification.

class UNNotificationAttachment

A media file associated with a notification.

class UNNotificationSound

The sound played when a notification is delivered.

struct UNNotificationSoundName

A string providing the name of a sound file.