[SWIFT] From iOS 14, UNNotificationPresentationOptions ".alert" has been split into ".banner" and ".list"

Introduction

There is a .alert in the push notification display options (ʻUNNotificationPresentationOptions), but it has been deprecated until iOS 14. Instead, it is divided into .banner and .list`, and I will introduce them because I investigated their behavior.

environment

Push notification behavior

It is as shown in the table below.

In .alert, notifications are sent to both the banner and notification center, but from iOS 14, you can use .banner and .list to make notifications come to only one of them.

UNNotificationPresentationOptions banner Notification Center
.alert Simulator Screen Shot - iPhone SE (2nd generation) - 2020-09-29 at 13.56.50.png SimulatorScreenShot-iPhoneSE(2ndgeneration)-2020-09-29at13.56.34.png
.banner Simulator Screen Shot - iPhone SE (2nd generation) - 2020-09-29 at 13.56.50.png SimulatorScreenShot-iPhoneSE(2ndgeneration)-2020-09-29at13.57.48.png
.list Simulator Screen Shot - iPhone SE (2nd generation) - 2020-09-29 at 13.58.49.png SimulatorScreenShot-iPhoneSE(2ndgeneration)-2020-09-29at13.56.34.png

Implementation

If iOS 13 or earlier is still supported, it is better to branch by OS version.

AppDelegate.swift


extension AppDelegate: UNUserNotificationCenterDelegate {
    func userNotificationCenter(
        _ center: UNUserNotificationCenter,
        willPresent notification: UNNotification,
        withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
        if #available(iOS 14.0, *) {
            completionHandler([[.banner, .list, .sound]])
        } else {
            completionHandler([[.alert, .sound]])
        }
    }
}

Bonus: Try push notifications in the simulator

The following articles helped me send push notifications to the iOS simulator. https://qiita.com/koogawa/items/85c0dd0abd2f1970c5fc

Thanks to this article, I was able to easily send push notifications and try them out.

in conclusion

From iOS 14, we found that we had more control over the display of notifications. By specifying only .list, you can use it like Android silent notification, and it seems convenient to master it.

If you find it useful, please let us know in the comments: relaxed:

Reference link

Recommended Posts

From iOS 14, UNNotificationPresentationOptions ".alert" has been split into ".banner" and ".list"