Class

UNNotificationAttachment

A media file associated with a notification.

Declaration

class UNNotificationAttachment : NSObject

Overview

Create a UNNotificationAttachment object when you want to include audio, image, or video content together in an alert-based notification. When creating the UNNotificationAttachment object, the file you specify must be on disk, and the file format must be one of the supported types.

You are responsible for supplying attachments before the system displays your notification's alert. For local notifications, add attachments when creating the notification’s content. For remote notifications, use a notification service app extension to download the attached files and then add them to the notification's content before delivery.

The system validates attachments before displaying the associated notification. If you attach a file to a local notification request that is corrupted, invalid, or of an unsupported file type, the system doesn't schedule your request. For remote notifications, the system validates attachments after your notification service app extension finishes. Once validated, attached files are moved into the attachment data store so that they can be accessed by all of the appropriate processes. Attachments located inside an app’s bundle are copied instead of moved.

Supported File Types

Table 1 lists the types of files you can include as an attachment and the supported file formats. The table also lists the maximum size allowed for attachments of each type. An image file may contain a static image or an animated image sequence.

Table 1

Supported attachment file types

Attachment

Supported file types

Maximum size

Audio

kUTTypeAudioInterchangeFileFormat

kUTTypeWaveformAudio

kUTTypeMP3

kUTTypeMPEG4Audio

5 MB

Image

kUTTypeJPEG

kUTTypeGIF

kUTTypePNG

10 MB

Movie

kUTTypeMPEG

kUTTypeMPEG2Video

kUTTypeMPEG4

kUTTypeAVIMovie

50 MB

When creating an attachment, you can specify optional details about how to present the thumbnail image for the image or movie. Use the UNNotificationAttachmentOptionsThumbnailClippingRectKey option to use only the specified portion of an image as a thumbnail. For animated images and movies, use the UNNotificationAttachmentOptionsThumbnailTimeKey option to select which frame to use for the thumbnail image.

The amount of storage space allocated for attachments is limited for each app. To delete attachments, use the methods of the UNUserNotificationCenter class to remove the notification requests that contain those attachments.

Topics

Creating an Attachment

init(identifier: String, url: URL, options: [AnyHashable : Any]? = nil)

Creates an attachment object from the specified file and options.

let UNNotificationAttachmentOptionsTypeHintKey: String

A hint about an attachment’s file type.

let UNNotificationAttachmentOptionsThumbnailHiddenKey: String

A Boolean value indicating whether the attachment’s thumbnail is hidden.

let UNNotificationAttachmentOptionsThumbnailClippingRectKey: String

The clipping rectangle for a thumbnail image.

let UNNotificationAttachmentOptionsThumbnailTimeKey: String

The frame number of an animation to use as a thumbnail image.

Getting the Attachment Contents

var identifier: String

The unique identifier for the attachment.

var url: URL

The URL of the file for this attachment.

var type: String

The UTI type of the attachment.

Relationships

Inherits From

See Also

Notification Content

class UNMutableNotificationContent

The editable content for a notification.

class UNNotificationContent

The uneditable content of a notification.

class UNNotificationSound

The sound played when a notification is delivered.

struct UNNotificationSoundName

A string providing the name of a sound file.