2017-04-21 112 views
0

当用户点击UILocalNotification时,我正尝试使用特定屏幕启动我的应用。这是我在AppDelegate代码:iOS UILocalNotification启动特定屏幕

// 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默认屏幕启动应用程序。如何在特定屏幕处于暂停状态时启动我的应用程序?

+0

当应用程序暂停时,通知正在被'didFinishLaunchingWithOptions'方法捕获。 – ridvankucuk

回答

1

做整个事情didFinishLaunching也因为这就是暂停的应用通知进来

例如

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

    } 
+0

谢谢,它的工作原理! – BadCodeDeveloper