iOS UILocalNotification启动特定屏幕

时间:2017-04-21 07:43:33

标签: ios swift uilocalnotification

当用户点按UILocalNotification时,我正尝试使用特定屏幕启动我的应用。这是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")
                    }
                }
            }
        }

当应用处于后台或非活动状态时,它可以正常工作,但当它处于暂停状态时,点击UILocalNotification会启动默认屏幕应用。如何在处于暂停状态时使用特定屏幕启动我的应用程序?

1 个答案:

答案 0 :(得分:1)

在didFinishLaunching中完成所有事情,因为暂停应用程序的通知就在这里

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])
        }

    }