1

我有一个应用程序,它的 tabBar 有 5 个选项卡,第一个让我们称之为“FirstView” - 具有以下代码:

    override func viewWillAppear(_ animated: Bool) {
            ...
            NotificationCenter.default.addObserver(self, selector: #selector(self.catchIt), name: NSNotification.Name(rawValue: "myNotif"), object: nil)
        }



        override func viewWillDisappear(_ animated: Bool) {
            ...
            NotificationCenter.default.removeObserver(self)
        }

    override func viewDidAppear(_ animated: Bool) {
            ...
            NotificationCenter.default.post(name: Notification.Name(rawValue: "myNotif"), object: nil, userInfo: userInfo as [AnyHashable: Any])
        }
    }

我在 didReceiveRemoteNotification 中也有代码,其中包含以下代码:

NotificationCenter.default.post(name: Notification.Name(rawValue: "myNotif"), object: nil, userInfo: userInfo as [AnyHashable: Any])

我得到了 UIViewController 扩展,其中有 catchIt() 并定义了重定向逻辑:

 func catchIt(_ userInfo: Notification) {
        let notification = userInfo.userInfo 
            if (path == "account.balance") {
                let vc: ProfileViewController = storyboard!.instantiateViewController(withIdentifier: "account.balance") as! ProfileViewController
                self.navigationController?.pushViewController(vc, animated: true)
//..and so on for other paths
      }
 }

此代码有 2 个问题: 1) 仅当最后打开的 View 是 FirstView 时,重定向才有效。如果是其他视图,那么在点击推送通知时我将永远不会被重定向

2)我在重定向时得到了我的历史记录,但它一直跟踪到 FirstView,即使目标控制器位于与 FirstViews 不同的另一个选项卡上(我尝试先重定向到所需选项卡,然后重定向到目标控制器,但仍然它提供了 FirstView 选项卡中的曲目)

我也尝试使用 didReceiveRemoteNotification - 将所有重定向逻辑写入其中,但它不起作用

非常感谢您的建议

4

1 回答 1

0

这里的问题是,在addObserver之前调用了post 通知。这就是为什么没有触发。

解决方案 :

不要在你的addObserverviewWillAppear中,而是在这样的init方法中这样做:

required init(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)!
    //Redirect for push notification
    NotificationCenter.default.addObserver(self, selector: #selector(self. catchIt), name: NSNotification.Name(rawValue: "myNotif"), object: nil)
}
于 2017-09-28T14:37:29.977 回答