我使用firebase将消息发送到ios设备,我进行调试,我已经在Appdelegate的func中收到了数据有效载荷
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any]) {
if let messageID = userInfo[gcmMessageIDKey] {
print("Message ID: \(messageID)")
}
我想根据数据来打开不同的视图控制器,这意味着当我单击消息时,将转到相应的视图控制器。 我在Appdelegate中使用了以下代码,但失败了
let sb = UIStoryboard(name: "Main", bundle: nil)
let otherVC = sb.instantiateViewController(withIdentifier: "UserNotLoginViewController") as! UserNotLoginViewController
self.window?.rootViewController = otherVC;
答案 0 :(得分:0)
在您的appDelegate中声明此函数,然后使用它更改rootViewController。
public func makeRootVC(vcName : String) {
let vc = UIStoryboard(name: "Main", bundle: Bundle.main).instantiateViewController(withIdentifier: vcName)
let navigation = UINavigationController(rootViewController: vc)
navigation.navigationBar.isHidden = true
self.window?.rootViewController = navigation
}
用法:
self.makeRootVC("YourViewControllerStoryboardID")
答案 1 :(得分:0)
当您在didReceiveRemoteNotification委托中接收通知时,然后调用该函数将pushview推送到nextviewcontroller。
func application(_ application: UIApplication, didReceiveRemoteNotification
data: [AnyHashable : Any]) {
let state: UIApplicationState = UIApplication.shared.applicationState
if state == .background {
// background
pushToPage(data: data)
}
}
func pushToPage(data:[AnyHashable : Any]){
if let appDelegate = UIApplication.shared.delegate as? AppDelegate,
let window = appDelegate.window {
let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
let nextViewController =
storyBoard.instantiateViewController(withIdentifier: "NextViewController") as! NextViewController
window.rootViewController = nextViewController
}
}
答案 2 :(得分:0)
在这里
func handlePushNotification(userInfo: [String: Any]) {
guard let notificationType = userInfo["nt"] as? String else {
return
}
if notificationType.toInt() == 1 {
self.navigateToViewController1()
} else if notificationType.toInt() == 2 {
self.navigateToViewController2()
}
}
对于导航,您可以使用以下功能
fileprivate func navigateToViewController1() {
if let rootViewController = self.window?.rootViewController as? UINavigationController {
if let _ = rootViewController.topViewController as? VC1 {
let vc = AppStoryboard.Main.viewController(viewControllerClass: VC3.self)
rootViewController.pushViewController(vc, animated: true)
}
}
}
fileprivate func navigateToViewController2() {
if let rootViewController = self.window?.rootViewController as? UINavigationController {
if let homeVC = rootViewController.topViewController as? VC2 {
}
}
}
还是,您遇到任何问题,请告诉我。