Receive parameterized PUSH notifications on iOS

As a premise of this article, we will assume that you have already received the PUSH notification.

References for implementing PUSH notifications: Set up the Firebase Cloud Messaging client app on iOS (https://firebase.google.com/docs/cloud-messaging/ios/client)

By default, the PUSH notification banner will not be displayed when the app is running. The value arrives at the next didReceiveRemoteNotification.


class AppDelegate: UIResponder, UIApplicationDelegate {

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {

        //abridgement...

        Messaging.messaging().delegate = self

        //abridgement...
}

extension AppDelegate: MessagingDelegate {

    func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
        print(userInfo)
    }

}

However, when UNUserNotificationCenter is connected, the above didReceiveRemoteNotification is no longer called, and the following func is now called. Also, depending on the value returned to completionHandler, the PUSH notification banner will be displayed even if the app is on the entire surface.

DidReceive was called when the app was tapped, whether it was running or not. You may not use didReceiveRemoteNotification in cases where there is a process to tap the PUSH notification.

extension AppDelegate : UNUserNotificationCenterDelegate {
    
    func userNotificationCenter(_ center: UNUserNotificationCenter,
                                willPresent notification: UNNotification,
                                withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
        let userInfo = notification.request.content.userInfo

        print(userInfo)
        
        if #available(iOS 14.0, *) {
            completionHandler([[.banner, .list, .sound]])
        } else {
            completionHandler([[.alert, .sound]])
        }
        
        // NOTE:Click here if you don't want to see push notifications
        // completionHandler([])
    }
    
    func userNotificationCenter(_ center: UNUserNotificationCenter,
                                didReceive response: UNNotificationResponse,
                                withCompletionHandler completionHandler: @escaping () -> Void) {
        let userInfo = response.notification.request.content.userInfo

        //Displayed as an alert to easily check userInfo when it is not started
        if let rootViewController = (UIApplication.shared.connectedScenes.first?.delegate as? SceneDelegate)?.window?.rootViewController {
            let alertController = UIAlertController(title: "userInfo", message: userInfo.description, preferredStyle: .alert)
            let okAction = UIAlertAction(title: "OK", style: .default, handler: nil)
            alertController.addAction(okAction)
            rootViewController.present(alertController, animated: true, completion: nil)
        }
                
        completionHandler()
    }
    
}

References

Recommended Posts

Receive parameterized PUSH notifications on iOS
How to send push notifications on AWS
Push notification permission pop-up does not appear on iOS14
Receive server notifications for iOS "automatic renewal subscription" in Ruby
[IOS] canOpenURL becomes false on iOS14
ATDD development on iOS (basic)