单击带有Reveal的推送通知,导航到特定视图

时间:2017-08-29 09:03:02

标签: ios swift firebase push-notification

我想在点击推送通知时导航到特定的视图控制器。

我已经在didReceiveRemoteNotification方法中编写了这段代码。

func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any]) {
        let storyboard = UIStoryboard(name: "Main", bundle: nil)
        let vc = storyboard.instantiateViewController(withIdentifier: "deals") as! FYIDealsVC
        let naviVC:UINavigationController? = self.window?.rootViewController?.revealViewController().frontViewController as? UINavigationController
        naviVC?.pushViewController(vc, animated: true)
}

但是它给了我这个让应用程序崩溃的错误。

  

致命错误:在解包可选值时意外发现nil

有很多帖子带有导航控制器或只是展示了viewcontroller。但我想通过揭示导航到特定的视图。

任何帮助都将受到高度赞赏。

2 个答案:

答案 0 :(得分:2)

这是另一个解决方案尝试这个: -

第一个选项: -

    let storyboard = UIStoryboard(name: "Main", bundle: nil)
     let vc = storyboard.instantiateViewController(withIdentifier: 
    "deals") as! FYIDealsVC

  // setup revelview controller and root with window 

    self.window?.rootViewController = //object of revelViewController
    self.window?.makeKeyAndVisible()

第二个选项: -

 //  in Landing Screen from where you can easily navigate to the target 
  viewcontroller :- 
登陆VC中的

: -

   viewdidload:- 
            NotificationCenter.default.addObserver(self, selector: 
    #selector(self.navigationForNotification(notification:)), name: 
   NSNotification.Name(rawValue:PushNavigationIdentifier), object: nil)


// Selector Method :-

 func navigationForNotification(notification:Notification) {
  let storyboard = UIStoryboard(name: "Main", bundle: nil)
    let vc = storyboard.instantiateViewController(withIdentifier: 
 "deals") as! FYIDealsVC
 self.navigationController?.pushViewController(vc, animated: true)  
}


 in appDelegate :- 
   func application(_ application: UIApplication, 
   didReceiveRemoteNotification userInfo: [AnyHashable: Any]) {
                  NotificationCenter.default.post(name: 
   NSNotification.Name(PushNavigationIdentifier), object: userInfo)

   }

答案 1 :(得分:0)

当您强行打开FYIDealsVC时,看起来会发生错误。 你能用吗

let vc = FYIDealsVC()

而不是

let vc = storyboard.instantiateViewController(withIdentifier: "deals") as! FYIDealsVC