我使用firebase向ios设备发送消息,我进行调试,我在func的Appdelegate中收到了数据有效负载
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any]) {
if let messageID = userInfo[gcmMessageIDKey] {
print("Message ID: \(messageID)")
}
我想做如何根据该数据打开不同的 View Controller ,这意味着当我单击消息时,我将转到相应的 View Controller 。 我在 Appdelegate 中使用了下面的代码但失败了
let sb = UIStoryboard(name: "Main", bundle: nil)
let otherVC = sb.instantiateViewController(withIdentifier: "UserNotLoginViewController") as! UserNotLoginViewController
self.window?.rootViewController = otherVC;
最佳答案
当您在 didReceiveRemoteNotification 委托(delegate)中收到通知时,调用该函数将 View 推送到下一个 View Controller 。
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
}
}
关于ios - 如何从 Appdelegate 打开 Viewcontroller?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54702713/