Ios 如何在Swift中的didReceiveRemoteNotification上从应用程序委托推送视图控制器?

Ios 如何在Swift中的didReceiveRemoteNotification上从应用程序委托推送视图控制器?,ios,swift,uiviewcontroller,apple-push-notifications,firebase-cloud-messaging,Ios,Swift,Uiviewcontroller,Apple Push Notifications,Firebase Cloud Messaging,我找不到任何明确的答案,我开始挣扎。到目前为止,我所得到的最接近于显示所需的视图控制器,但显然是以模式显示,而不是使用选项卡栏或导航栏 我在我的AppDelegate中有didReceiveMemoteNotification功能来尝试和处理Firebase推送通知。基本上,我希望能够根据我在Firebase控制台上分配的键值对,自动切换到我应用程序中的特定视图控制器 到目前为止,我的应用程序代理如下所示: AppDelegate 如您所见,我已将名称转换为字符串,以便处理它,然后根据结果切换它

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

我在我的
AppDelegate
中有
didReceiveMemoteNotification
功能来尝试和处理Firebase推送通知。基本上,我希望能够根据我在Firebase控制台上分配的键值对,自动切换到我应用程序中的特定视图控制器

到目前为止,我的应用程序代理如下所示:

AppDelegate 如您所见,我已将
名称
转换为
字符串
,以便处理它,然后根据结果切换它。它将实例化初始视图控制器,它将始终是主菜单,然后经过5秒的延迟(这纯粹是出于测试原因),我希望它推送到Controller1VC

AppDelegate或视图控制器方法? 我说的对吗?还是最好将结果传递给MainMenuVC并实现一些逻辑,然后执行一个相对简单的
performsgue

作为一个边栏,我从Firebase接收和读取的数据非常好,我只需要让它开始工作


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

对于您的情况,我建议将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)

在Shu Khan的帮助下,我成功地让它在我需要的时候工作,我在这个帖子中找到了答案:

我的初始视图控制器是一个选项卡栏,因此,这是适合我的正确代码:

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")
    }
}
因此,此代码基本上将
选项卡栏
转换为
UINavigationController
,从而允许
pushViewController
工作


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

谢谢你的回答,它为我指明了正确的方向,所以我四处游荡,找出了我的错误所在!
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")
    }
}