当用户使用iOS Swift轻击推送通知时,在特定视图中打开应用程序

我的应用程序允许远程推送通知给用户。 当用户点击推送通知时,如何使其在特定的视图控制器中打开? 我希望应用程序打开并根据收到的推送通知导航到特定的视图控制器。

要做到这一点,你需要为你的应用程序可以打开的每个ViewController设置一个identifier ,然后检查application:didFinishLaunchingWithOptions:launchOptions参数中的payload application:didFinishLaunchingWithOptions:在你的AppDelegate这是执行此操作的步骤:

  1. 在您的PFPush ,使用setData将标识添加到您的有效负载中的一个键: notification.setData(["alert":"your notification string", "identifier":"firstController"])

  2. 通过select每个ViewController并更改以下值来设置identifier

设置故事板ID

  1. 使您的推送通知使用密钥identifier在其payload发送故事板ID
  1. 检查应用程序中的ID:didFinishLaunchingWithOptions:在函数的末尾添加以下内容:
 if let payload = launchOptions?[UIApplicationLaunchOptionsRemoteNotificationKey] as? NSDictionary, identifier = payload["identifier"] as? String { let storyboard = UIStoryboard(name: "Main", bundle: nil) let vc = storyboard.instantiateViewControllerWithIdentifier(identifier) window?.rootViewController = vc } 

在AppDelegate中,您将获得一个委托callback“didFinishLoading”或“didReceivePushNotification”方法(基于您的应用程序在后台或前台)。 在该方法中,获取最顶层视图控制器的实例,然后创build您想要显示的特定视图控制器,并从最顶层的视图控制器呈现/推送。

  UILocalNotification *notification = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey]; if (notification) { [self application:application didReceiveRemoteNotification:(NSDictionary*)notification]; }