0
votes

I am using FCM to send push notifications, and i am using the following method in AppDelegate to receive data sent with the notification.

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




        if let firstMessage = userInfo["url"] as? String, let secondMessage = userInfo["category"] as? String {
            print("message1: \(firstMessage)")
            print("message2: \(secondMessage)")

            let mainStoryBoard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
            let mainVC = mainStoryBoard.instantiateViewController(withIdentifier: "MainViewController") as! MainViewController

            mainVC.url = firstMessage
            mainVC.category = secondMessage
            self.window?.rootViewController = mainVC



        }else {

            print("no data received!")
        }



        completionHandler(UIBackgroundFetchResult.newData)
    }

The data is successfully received when app in foreground or in background, but when app is terminated (killed), that notification is received but with no data, as i understand the "didReceiveRemoteNotification", is not called when app is terminated and instead i have to write code in "didFinishLaunchingWithOptions" to display data received with Push notifications. Any idea how to do so? Thanks

1

1 Answers

0
votes

iOS apps can't received data payload when app is terminated, it is a limitation of iOS. Instead what you can do is set both notification and data payload in FCM and. So if your app is terminated, you still see a notification, and when user taps on the notification, you can receive data payload in didReceiveRemoteNotification function as userInfo, like you are getting when app is in foreground.