ios - 打开特定 View 的通知

标签 ios swift push-notification apple-push-notifications

我正在使用推送通知在 Swift 中开发一个应用程序。到目前为止,我的 AppDelegate 中有以下代码:

  func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) {
    println("Received alert and opened it")
    debugPrintln(userInfo)

    if application.applicationState == UIApplicationState.Active {
      // App in foreground
      println("App in foreground already")
    } else {
      // App in background
      if let tripId = (userInfo["trip"] as? String)?.toInt() {
        println(tripId)
        Trips.load(tripId) { (responseCode, trip) in
          debugPrintln("Got the trip")
          if let trip = trip {
            if let window = self.window {
              if let rootViewController = window.rootViewController {
                if let storyboard = rootViewController.storyboard {
                  let viewController = storyboard.instantiateViewControllerWithIdentifier("Trip") as! TripViewController
                  viewController.trip = trip
                  rootViewController.presentViewController(viewController, animated: true, completion: nil)
                } else {
                  println("No storyboard")
                }
              } else {
                println("No root view controller")
              }
            } else {
              println("No window")
            }
          }
        }
      } else {
        println("Failed to get trip id")
      }
    }
  }

Storyboard的构造是,当应用程序首次打开时,它会打开 LoginViewController,后者会检查登录状态并重定向到包含行程列表的 NavigationController。从列表中,用户可以点击行程以打开 TripViewController(请参阅 screenshot)。

当我运行我的应用程序并测试点击推送通知时,该应用程序会加载行程列表并且我会在我的控制台中看到以下日志:

2015-09-04 09:50:07.158 GoDriver[883:377922] Warning: Attempt to present <GoDriver.TripViewController: 0x15f5b260> on <GoDriver.LoginViewController: 0x15d910e0> whose view is not in the window hierarchy!

我是否必须加载我的导航 Controller 并使用 TripViewController 填充它?

最佳答案

如果您正在使用 UIStoryBoard 并使用 initialViewController,iOS 会自动执行必要的操作,即加载它,如果需要创建 navigationController 并将其加载到窗口。

但是在这种情况下,您需要手动执行此操作。您需要创建一个 UINavigationController,用您的 TripViewController 填充它并用 UIWindow Hook 它。

关于ios - 打开特定 View 的通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32400105/

相关文章:

ios - 选择注释时我可以禁用 map 平移吗

ios - 如何在 iOS 的触地得分上添加不透明度?

ruby - 使用 Swift,如何使用 SHA1 创建 OpenSSL HMAC 摘要,然后使用严格的encode64 进行编码

ios - 无法在 Swift 3.0 中将 View Controller 显示为弹出 Controller

ios - 为什么自定义 UITableViewCell 不显示任何属性?

ios - 发布带有标签的照片时获取 "Unsupported FBSDKGraphRequest attachment"

ios - 更改动态 TableView 中的特定单元格,Swift

ios - node-apn 库中出现奇怪的语法错误

android - 使用 node-gcm 将负载通知发送到 android 设备

php - 用于使用 Google Cloud 消息传递的 API 的数据结构