When my App is not running and receives a Push Notification, if I click on that notification, the App is launched - but then it doesn\'t prompt the user with the Alert-View
Perform actions after notification is received in the terminated state - iOS 13 - Scene Delegate
Recently I came across an issue where I received remote push notification when my app was in the terminated state. In the latest iOS versions Scene delegate is responsible to handle view life cycle methods rather than App Delegate.
Older iOS versions - Handled by App Delegate When the app is terminated and remote push notification is received the payload is reflected in the didfinishLaunchingWithOptions method of App delegate. Using the launch parameter of this method it’s possible to get the payload data and perform any interaction.
New iOS version - Handled by Scene Delegate Similarly when the app is terminated and remote push notification is received the payload is reflected in the scene(willConnectTo session) of the scene delegate. Using the connectingOption parameter of this method it’s possible to get the payload data and perform any interaction.
Hint: To perform any interaction or pass this payload to another view controller once the view controller is set as root view controller keep a delay of some seconds to pass the data.
Example code:
guard let windowScene = (scene as? UIWindowScene) else { return }
window = UIWindow(frame: windowScene.coordinateSpace.bounds)
window?.windowScene = windowScene
if defaults.string(forKey: UserDefaultsKeys.TenantID.rawValue) != nil && connectionOptions.notificationResponse != nil {
let rootViewController = UINavigationController(rootViewController: DashboardVC())
window?.rootViewController = rootViewController
window?.makeKeyAndVisible()
DispatchQueue.main.asyncAfter(deadline: .now() + 1.0) {
NotificationCenter.default.post(
name: .passApnsDataToDashboardNotification,
object: nil,
userInfo: connectionOptions.notificationResponse?.notification.request.content.userInfo)
}
}