App is not launching when push notification is tapped on iOS, Swift

Viewed 967

I have now integrated push notifications in my app using FirebaseNotifications. For this I use a NotificationServiceExtension for processing (CoreData, ...) and the usual methods, such as didReceiveRemoteNotification, willPresent or didReceive in AppDelegate.

The processing of the content and the display of the push notification works perfectly in all status of the app - in the background and also in the foreground.

If the app is in the foreground or in the background, but not killed, tapping on the push notification also works and the didReceive method is called and I can navigate to the desired content.

If the app is completely killed, a tap on the push notification a few seconds later starts the app, but the usual launch screen (splash screen) is not called and nothing else happens - the app remains on a white screen.

At first I suspected I had forgotten to call a completionHandler, but they are all stored and I don't find an error anywhere.

Can someone please help me, where there could still be a problem that the splash screen, then the AppDelegate and my normal ViewController, is called up.

Thank you - here is my code in the AppDelegate. Unfortunately I don't have a log because the app has to be completely killed and I therefore have no output in the Xcode console.

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

func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
    completionHandler([[.alert, .badge, .sound]])
}

func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
    
    defer {
        completionHandler()
    }
    
    if response.actionIdentifier == "friends" {
        print("Open button was pressed.")
        
        // TODO: - Deep link to friends
    }
    completionHandler()
}
1 Answers

To handle that, you need to add functionality in didFinishLaunchingWithOptions

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

    if let launchOptions = launchOptions {
        if let info = launchOptions[UIApplication.LaunchOptionsKey.remoteNotification] {
            let dic = info as! NSDictionary
            print("dic from closed app",dic)
            PushNotificationResponse(Data: dic)
        }
    }

    return true

}

In didReceiveRemoteNotification have below code.

func application(_ application: UIApplication, didReceiveRemoteNotification data: [AnyHashable : Any]) {
    // Print notification payload data
    print("Push notification received: \(data)==\(application.applicationState)")
    
    switch application.applicationState {
        case .active:
            print("active mode")
            // PushNotificationResponse(Data:data as NSDictionary)
            break
        case .inactive:
            print("inactive mode")
            PushNotificationResponse(Data:data as NSDictionary)
            break
        case .background:
            print("background mode")
            PushNotificationResponse(Data:data as NSDictionary)
            break
        default:
            break
    }
}

This is where you will handle on click events on push at one place.

func PushNotificationResponse(Data:NSDictionary){
    print("PushNotificationResponse\(Data)")
    // handle push code here
}
Related