Search code examples
iosswiftuilocalnotification

iOS UILocalNotification launch specific screen


I'm trying to launch my app with specific screen when user taps UILocalNotification. Here is my code in AppDelegate:

 // MARK: - Local Notification
        func application(_ application: UIApplication, didReceive notification: UILocalNotification)
        {
            if (application.applicationState == UIApplicationState.active)
            {
                print("Active")
            }
            else if (application.applicationState == UIApplicationState.background)
            {
                print("Background")
            }
            else if (application.applicationState == UIApplicationState.inactive)
            {
                print("Inactive")
                print(notification.userInfo as Any)
                self.redirectToPage(userInfo: notification.userInfo as! [String : String])
            }
        }

        func redirectToPage(userInfo: [String : String]!)
        {
            if userInfo != nil
            {
                if let pageType = userInfo["TYPE"]
                {
                    if pageType == "Page1"
                    {
                        let storyboard = UIStoryboard(name: "Main", bundle: nil)
                        self.window?.rootViewController = storyboard.instantiateViewController(withIdentifier: "promoVC")
                    }
                }
            }
        }

It works fine when app is in background or inactive state, but when it is suspended, tapping UILocalNotification launches app with default screen. How can I launch my app with specific screen when it is in suspended state?


Solution

  • do the whole thing in didFinishLaunching too as that's where notifications for suspended apps come in

    e.g.

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey : Any]?) -> Bool {
            //allow notifs
            let types : UIUserNotificationType = [.alert]
            let settings = UIUserNotificationSettings(types: types, categories: nil)
            application.registerUserNotificationSettings(settings)
    
            //forward notif, if any
            if let note = launchOptions?[UIApplicationLaunchOptionsKey.localNotification] as? UILocalNotification {
                handleNotification(note: note, fromLaunch: true)
            }
    }
    
     func application(_ application: UIApplication, didReceive note: UILocalNotification) {
                handleNotification(note: note, fromLaunch: false)
     }
    
     func handleNotification(note:UILocalNotification, launch:Bool) {
           if (application.applicationState == UIApplicationState.active)
            {
                print("Active")
            }
            else if (application.applicationState == UIApplicationState.background)
            {
                print("Background")
            }
            else if (application.applicationState == UIApplicationState.inactive)
            {
                print("Inactive")
                print(notification.userInfo as Any)
                self.redirectToPage(userInfo: notification.userInfo as! [String : String])
            }
            else {
                print("other ;)")
                print(notification.userInfo as Any)
                self.redirectToPage(userInfo: notification.userInfo as! [String : String])
            }
    
        }