Class

UNNotificationAttachment

Manages media content associated with a notification.

Overview

A UNNotificationAttachment object contains audio, image, or video content to display alongside the notification content. Your app always supplies attachments. For local notifications, the app adds attachments when creating the rest of the notification’s content. To add attachments to a remote notification, use a notification service extension to modify the notification content before it is delivered. For more information about implementing a notification service extension, see UNNotificationServiceExtension.

You create attachments using the init(identifier:url:options:) method of this class. You must specify the contents of an attachment using a file on disk, and the file format must be one of the supported types. After creating the attachment, assign it to the attachments property of your notification’s content object. (For remote notifications, you must do this from your service extension.)

The system validates the content of attached files before scheduling the corresponding notification request. If an attached file is corrupted, invalid, or of an unsupported file type, the notification request is not scheduled for delivery. Once validated, attached files are moved into the attachment data store so that they can be accessed by 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 create a thumbnail image based on the specified rectangle of the original image. For animated images and movies, use the UNNotificationAttachmentOptionsThumbnailTimeKey option to selection 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.

Symbols

Creating an Attachment

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

Creates an attachment object from the specified file and options.

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.

Constants

Attachment Attributes

Constants describing information about an attachment.

Relationships

Inherits From