简体   繁体   中英

iOS app loads buggy when launching from a remote notification

My iOS app launches buggy (no launch image, safe areas aren't observed, tab bar doesn't show, etc.) when it's opened from a remote notification (when the user taps a push notification to open the app from a terminated state). I don't want the push notification to have any special functionality at all, I want it as plain as possible.

This is how I have my notifications configured in the App Delegate:

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

private func setupUserNotifications() {
    let notifications = UNUserNotificationCenter.current()
    let connectionMessages = UNNotificationCategory(identifier: "connectionMessageNotification",
                                                    actions: [],
                                                    intentIdentifiers: [],
                                                    options: UNNotificationCategoryOptions.init())
    let interactionMessages = UNNotificationCategory(identifier: "interactionMessageNotification",
                                                     actions: [],
                                                     intentIdentifiers: [],
                                                     options: UNNotificationCategoryOptions.init())
    
    notifications.delegate = self
    notifications.setNotificationCategories([connectionMessages, interactionMessages])
    
    notifications.requestAuthorization(options: [.alert, .sound, .badge]) { (granted, error) in
        if granted {
            DispatchQueue.main.async {
                UIApplication.shared.registerForRemoteNotifications()
            }
        } else {
            print(if: error)
        }
    }
}

And this is how I handle the delegate (doing nothing and just calling completion):

func userNotificationCenter(_ center: UNUserNotificationCenter,
                            didReceive response: UNNotificationResponse,
                            withCompletionHandler completionHandler: @escaping () -> Void) {
    print("did tap remote notification")
    completionHandler()
}

Is there a step I'm missing? Am I supposed to implement application(_:willFinishLaunchingWithOptions:) ? What I don't understand is that if the user launches the app by tapping a push notification, shouldn't the app just launch normally without having to take any extra steps?

The problem was that my app was configured to receive background push notifications and it would launch the app in the background (before the user tapped on the notification) and so when the user did tap on the notification to launch the app it would sometimes, I guess, catch it in some intermediate state and the UI would look all funky. I assume that there are extra steps that one would need to take to handle launching the app through a background notification. Regardless, by disabling background notifications, the app now launches when the user taps on the push notification as normal. Whew.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM