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