2017-10-18 6 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

감사하지만 로직을 얻었습니다. 메신저가 충돌을 일으켰습니다. 아이디어를 알리는 테스트를 위해 앱을 종료해야하므로 디버거에 연결되어 있지 않을 때 충돌을 어떻게 진단 할 수 있습니까? – jackdm

관련 문제