如何在Swift中的didReceiveRemoteNotification上从App Delegate推送视图控制器?

时间:2018-06-30 00:23:28

标签: ios swift uiviewcontroller apple-push-notifications firebase-cloud-messaging

我找不到任何明确的答案,我开始挣扎。到目前为止,我最接近的是present所需的视图控制器,但显然它是模态显示的,而不是用选项卡栏或导航栏显示。

我在AppDelegate中拥有didReceiveRemoteNotification函数来尝试处理Firebase推送通知。本质上,我希望能够根据我在Firebase控制台上分配的键值对自动选择应用中的特定视图控制器。

到目前为止,我的应用程序委托看起来像这样:

AppDelegate

    func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {

        let info = userInfo as NSDictionary
        let name: String = info.value(forKey: "view-controller") as! String

    switch name {
    case "controller1":
        print("controller1")
        let storyBoard = UIStoryboard.getMainStoryboard().instantiateInitialViewController()
        UIApplication.shared.delegate!.window!!.rootViewController = storyBoard
        DispatchQueue.main.asyncAfter(deadline: .now() + 5.0) {
            print("Time now reached")
            let storyboard = UIStoryboard(name: "Main", bundle: nil)
            let controller1VC = storyboard.instantiateViewController(withIdentifier: "controller1VC") as! Controller1VC
            self.window?.rootViewController?.present(controller1VC, animated: true)
        }
    default:
        print("Nothing")
    }

}

// and an extension I have...

extension UIStoryboard {
public class func getMainStoryboard() -> UIStoryboard {
    return UIStoryboard(name: "Main", bundle: nil)
}
}

因此,如您所见,我已经将name转换为String,以便可以处理它,然后根据结果进行切换。它将实例化始终为MainMenuVC的初始视图控制器,然后经过5秒钟的延迟(纯粹出于测试原因而存在),我希望它推送至Controller1VC。

AppDelegate还是View Controller方法?

我在这里正确吗?还是最好将结果传递给MainMenuVC并实现一些逻辑,然后执行相对简单的performSegue

作为补充,我从Firebase接收和读取数据完全正常,我只需要获取它即可开始工作!

提前感谢您提供的任何指导。

2 个答案:

答案 0 :(得分:2)

在您的情况下,我建议将rootVC设为UINavigationController并执行

let storyboard = UIStoryboard(name: "Main", bundle: nil)

let controller1VC = storyboard.instantiateViewController(withIdentifier: "controller1VC") as! Controller1VC

let nav = self.window?.rootViewController as! UINavigationController

nav.pushViewController(controller1VC, animated: true)

答案 1 :(得分:2)

Sh_Khan 和我在该线程中找到的答案:How can I show ViewController in UITabBarController?

的帮助下,我设法使其按需运行

我的初始视图控制器是tabBar,因此,这对我来说是正确的代码:

func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {

    let info = userInfo as NSDictionary
    let name: String = info.value(forKey: "view-controller") as! String

    switch name {
    case "controller1":
            let storyboard = UIStoryboard.init(name: "Main", bundle: nil)
            let controller1VC = storyboard.instantiateViewController(withIdentifier: "controller1VC") as! Controller1VC
            let tabBar = self.window?.rootViewController as? UITabBarController
            let nav = tabBar?.selectedViewController as? UINavigationController
            nav?.pushViewController(controller1VC, animated: true)
    default:
        print("Nothing")
    }
}

因此,这段代码实际上会将tabBar转换为UINavigationController,从而允许pushViewController起作用。

我希望这对其他人有帮助。