Instance Method

userNotificationCenter(_:willPresent:withCompletionHandler:)

Called when a notification is delivered to a foreground app.

Declaration

optional func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void)

Parameters

center

The notification center that received the notification.

notification

The notification that is about to be delivered. Use the information in this object to determine an appropriate course of action. For example, you might use the arrival of the notification to fetch new content or update your app’s interface.

completionHandler

The block to execute with the presentation option for the notification. Always execute this block at some point during your implementation of this method. Specify an option indicating how you want the system to alert the user, if at all. This block has no return value and takes the following parameter:

options

The option for notifying the user. Specify UNNotificationPresentationOptionNone to silence any alerts. Pass other values to specify which types of alerts you want to allow. For information about the available alert options, see UNNotificationPresentationOptions.

Discussion

If your app is in the foreground when a notification arrives, the notification center calls this method to deliver the notification directly to your app. If you implement this method, you can take whatever actions are necessary to process the notification and update your app. When you finish, execute the completionHandler block and specify how you want the system to alert the user, if at all.

If your delegate does not implement this method, the system silences alerts as if you had passed the UNNotificationPresentationOptionNone option to the completionHandler block. If you do not provide a delegate at all for the UNUserNotificationCenter object, the system uses the notification’s original options to alert the user.