打开特定视图的通知

时间:2015-09-04 14:04:38

标签: 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")
      }
    }
  }

构建故事板时,应用程序首次打开时,会打开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!

我是否必须加载导航控制器并使用TripViewController填充它?

1 个答案:

答案 0 :(得分:1)

如果您正在使用protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); RelativeLayout main = (RelativeLayout)findViewById(R.id.main); RelativeLayout.LayoutParams squarePosition = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT); GameToken square1 = new GameToken(this,GameToken.SQUARE, fieldHeight,fieldWidth); squarePosition.addRule(RelativeLayout.ALIGN_PARENT_LEFT); squarePosition.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM); main.addView(square1, squarePosition); System.out.println(square1.getX()); //Prints '0.0' System.out.println(square1.getY()); //Prints '0.0' 并使用UIStoryBoard,iOS会自动完成需要,即加载它,如果需要创建navigationController并将其加载到窗口。

但是在这种情况下,您需要手动执行此操作。您需要创建一个UINavigationController,使用TripViewController填充它并将其与initialViewController挂钩。

相关问题