2017-01-11 7 views
0

My appには2種類の初期状態があります。ダッシュボード&他のものが含まれてログインした後iOS 10でプッシュ通知を表示する際の問題Swift 3

  1. ログイン
  2. 。その

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { 
         let userDataExist = CommonUserFunction.isUserDataExist() as Bool 
    
         if userDataExist == true { 
    
          let homeViewController = self.window?.rootViewController?.storyboard?.instantiateViewController(withIdentifier: "HomeView") as? HomeViewController 
          let navigationController = UINavigationController() 
          navigationController.viewControllers = [homeViewController!] 
    
          self.window!.rootViewController = navigationController 
         } 
         else{ 
    
          let loginController = self.window?.rootViewController?.storyboard?.instantiateViewController(withIdentifier: "LoginView") as? LoginController 
          let navigationController = UINavigationController() 
          navigationController.viewControllers = [loginController!] 
    
          self.window!.rootViewController = navigationController 
         } 
    
         self.window?.makeKeyAndVisible() 
    
         return true 
    } 
    

    ようAppdelegateからビューを切り替えたとき、私はプッシュ通知を処理しなければならないとき、私は問題に直面しています細かい作業

マイアプリ。私は私が通知トレイからの通知をタップするとアプリが前景または背景であるのに対し、これら2つの方法が発射さ

@available(iOS 10.0, *) 
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (_ options: UNNotificationPresentationOptions) -> Void) { 
    print("Handle push from foreground") 
    // custom code to handle push while app is in the foreground 

    print(notification.request.content.userInfo) 
} 

@available(iOS 10.0, *) 
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping() -> Void) { 

    // custom code to handle push while app is in the foreground 
    print(response.notification.request.content.userInfo) 
} 

これらの方法でのiOS 10にプッシュ通知を受信しました。通知をタップしたら、私は詳細ページを表示する必要があります。私は私がdidFinishLaunchingWithOptionsメソッド

@available(iOS 10.0, *) 
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping() -> Void) { 

    let pushResponse = response.notification.request.content.userInfo as NSDictionary 

    let rtype = pushResponse.object(forKey: "rtype") as? String 
    let rid = pushResponse.object(forKey: "rid") as? String 

    if rtype != nil { 

     if rtype == "5" { 

      if rid != nil { 
       let noticeDetailsViewController = self.storyboard?.instantiateViewController(withIdentifier: "NoticeDetailsView") as? NoticeDetailsController 
       noticeDetailsViewController!.id = rtype! 
       noticeDetailsViewController?.fromPush = true 
       let navigationController = UINavigationController() 
       navigationController.viewControllers = [noticeDetailsViewController!] 
       self.window!.rootViewController = navigationController 
      } 
     } 
    } 
} 

私はそのアプリのクラッシュを行うたびに前にしたもののような詳細ページを設定しようとしました。これを克服する方法。助けてください。前もって感謝します。

+0

あなたのアプリがクラッシュする –

+0

userNotificationCenterメソッドの詳細ビューをrootviewControllerとして設定すると、 –

+0

あなたのクラッシュログは何ですか? 見せてもらえますか ? –

答えて

0

最後に、Observerデザインパターンを使用してこれを解決しました。アプリがバックグラウンドで動作しているとき

は&はあなたのViewController

NotificationCenter.default.addObserver(self, selector: #selector(self.getPushResponse), name: NSNotification.Name(rawValue: "getPush"), object: nil) 

取得するためにコードの下、この通知のinitを受信するには、その

@available(iOS 10.0, *) 
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping() -> Void) { 

    let pushResponse = response.notification.request.content.userInfo as NSDictionary 

    let rtype = pushResponse.object(forKey: "rtype") as? String 
    let rid = pushResponse.object(forKey: "rid") as? String 

    if rtype != nil { 

     if rtype == "5" { 

      if rid != nil { 

       NotificationCenter.default.post(name: Notification.Name(rawValue: "getPush"), object: pushResponse) 
      } 
     } 
    } 
} 

のように、ここからの通知を投稿通知トレイに通知をタップ下のコードからの応答

func getPushResponse(_ notification: Notification){ 

    if let info = notification.object as? NSDictionary { 
     let rid = info.object(forKey: "rid") as? String 
     let noticeDetailsViewController = self.storyboard?.instantiateViewController(withIdentifier: "NoticeDetailsView") as? NoticeDetailsController 
     noticeDetailsViewController?.id = rid! 
     noticeDetailsViewController?.fromPush = true 
     self.navigationController?.pushViewController(noticeDetailsViewController!, animated: true) 
    } 
} 

アプリがバックグラウンド&に何のインスタンスを持っていない場合はその

deinit { 
    NotificationCenter.default.removeObserver(self) 
} 

のようなのViewControllerからオブザーバーを削除することを忘れないでくださいには、インスタンスを持っていない通知を表示する方法を、次にトレイに通知をタップ。これは次のようにあなたの初期のViewControllerは、このキーで確認してください存在したりしませAppDelegate

let prefs: UserDefaults = UserDefaults.standard 
    if let remoteNotification = launchOptions?[UIApplicationLaunchOptionsKey.remoteNotification] as? NSDictionary { 
     prefs.set(remoteNotification as! [AnyHashable: Any], forKey: "startUpPush") 
     prefs.synchronize() 
    } 

にそのように扱うことができるよう

let prefs:UserDefaults = UserDefaults.standard 
    if prefs.value(forKey: "startUpPush") != nil { 

     let pushResponse = prefs.object(forKey: "startUpPush") as! NSDictionary 
     NotificationCenter.default.post(name: Notification.Name(rawValue: "getPush"), object: pushResponse) 
    } 

はその

ようdetailsControllerを示した後、このキーを削除することを忘れないでください。
if fromPush == true { 
     let prefs: UserDefaults = UserDefaults.standard 
     if prefs.value(forKey: "startUpPush") != nil { 
      prefs.removeObject(forKey: "startUpPush") 
      prefs.synchronize() 
     } 
    } 
関連する問題