2017-10-18 27 views
0

我有我的應用程序默認啓動,但是在某些情況下,我希望應用程序通過推送通知它提供了一個有效載荷識別網頁的加載打開。設置根視圖控制器不同,如果應用程序通過推送通知打開?

的問題我已經是有效載荷的邏輯是在推送通知didRecieve響應處理,我也處理的應用程序在didFinishLaunchingWithOptions推出。

我該如何阻止兩個衝突,因爲目前它似乎崩潰的應用程序。

本質上,我想根據通知中的有效負載更改啓動時的根。

目前我有:

func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping() -> Void) { 
    let userInfo = response.notification.request.content.userInfo 
    // map 
    let conferenceID = userInfo["cid"] as! String 
    self.pushNotifiedEventID = conferenceID 
    self.defaultEvent = conferenceID 
    if let p = userInfo["p"] as? [String : String] { 
     let pageID = p["id"]! 
     let title = p["title"]! 
     let type = p["type"]! 
    } 
    completionHandler() 
    let hostViewController = HostViewController() 
    self.window?.rootViewController = hostViewController 
} 

也:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { 
    if #available(iOS 10.0, *) { 
     center.delegate = self 
     center.requestAuthorization(options: [.badge, .alert , .sound]) { (granted, error) in 
      if granted { 
       DispatchQueue.main.async(execute: { 
        UIApplication.shared.registerForRemoteNotifications() 
       }) 
      } 
     } 
    } else { 
     let type: UIUserNotificationType = [UIUserNotificationType.badge, UIUserNotificationType.alert, UIUserNotificationType.sound] 
     let setting = UIUserNotificationSettings(types: type, categories: nil) 
     UIApplication.shared.registerUserNotificationSettings(setting) 
     UIApplication.shared.registerForRemoteNotifications() 
    } 
    // root VC init + push 
    window = UIWindow() 
    let loginViewController = LoginViewController.init(loginView: LoginView(frame: UIScreen.main.bounds)) 
    self.navigationController = UINavigationController(rootViewController: loginViewController) 
    window?.rootViewController = navigationController 
    window?.makeKeyAndVisible() 
    return true 
} 

回答

0

在你的AppDelegate類,創建一個方法:

func configureRootView(isComingFromNotification: Bool) { 
    if isComingFromNotification { 
    self.window?.rootViewController = HostViewController() 
    } 
    else { 
    let loginViewController = LoginViewController.init(loginView: LoginView(frame: UIScreen.main.bounds)) 
    self.navigationController = UINavigationController(rootViewController: loginViewController) 
    window?.rootViewController = navigationController 
    } 
} 

你要從你的func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping() -> Void)調用此方法和func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool方法。

+0

感謝我得到的邏輯,但即時通訊setill越來越崩潰,任何想法如何,我可以診斷崩潰時,我不能運行插入到調試器,我有權終止該應用測試的通知 – jackdm

相關問題