Instance Method

didReceive(_:withContentHandler:)

Asks you to make any needed changes to the notification and notify the system when you're done.

Declaration

func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void)

Parameters

request

The original notification request. Use this object to get the original content of the notification.

contentHandler

The block to execute with the modified content. This block has no return value and takes the following parameter:

contentToDeliver

A UNNotificationContent object with the content to be displayed to the user.

Discussion

Override this method and use it to modify the UNNotificationContent object that was delivered with the notification. At some point during your implementation, execute the contentHandler block and pass it your modified content. If you decide not to modify the content, call the contentHandler block with the original content from the request parameter.

You can modify any of the content from the original request. You might customize the content for the current user or replace it altogether. You can use this method to download images or movies and add them as attachments to the content. You may also modify the alert text as long as you do not remove it. If the content object does not contain any alert text, the system ignores your modifications and delivers the original notification content.

Your extension has a limited amount of time (no more than 30 seconds) to modify the content and execute the contentHandler block. If you do not execute that block in a timely manner, the system calls your extension’s serviceExtensionTimeWillExpire() method to give you one last chance to execute the block. If you do not, the system presents the notification’s original content to the user.

See Also

Processing Notifications

func serviceExtensionTimeWillExpire()

Tells you that your extension is about to be terminated.

Beta Software

This documentation contains preliminary information about an API or technology in development. This information is subject to change, and software implemented according to this documentation should be tested with final operating system software.

Learn more about using Apple's beta software