ios - 如何从 Appdelegate 打开 Viewcontroller?

标签 ios swift firebase push-notification firebase-cloud-messaging

我使用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/

相关文章:

ios - 从 NSURLConnection 迁移到 NSURLSession

iOS 中的 HTML 电子邮件

iOS - 如何使用泛型在 Swift 中编写 DecodeHelper 类?

ios - 以编程方式向下移动 TableView ? ( swift )

swift - 使用 if 语句访问结构中的枚举值

ios - Swift Firebase - 使用 queryOrdered(byChild : ). queryEqual(toValue:) 时如何获取所有 k/v

ios - 将数据写入 firebase 而不覆盖它。 swift

node.js - 从 "firebase serve"提供的 HTTP 函数永远卡在 "error: SUPERVISOR clientError"

iOS:键入时搜索建议

ios - 关闭 View Controller 后 Swift 4 Call ViewWillAppear