当用户使用iOS Swift轻击推送通知时,在特定视图中打开应用程序
我的应用程序允许远程推送通知给用户。 当用户点击推送通知时,如何使其在特定的视图控制器中打开? 我希望应用程序打开并根据收到的推送通知导航到特定的视图控制器。
要做到这一点,你需要为你的应用程序可以打开的每个ViewController
设置一个identifier
,然后检查application:didFinishLaunchingWithOptions:
的launchOptions
参数中的payload
application:didFinishLaunchingWithOptions:
在你的AppDelegate
这是执行此操作的步骤:
-
在您的
PFPush
,使用setData
将标识添加到您的有效负载中的一个键:notification.setData(["alert":"your notification string", "identifier":"firstController"])
-
通过select每个
ViewController
并更改以下值来设置identifier
- 使您的推送通知使用密钥
identifier
在其payload
发送故事板ID
- 检查应用程序中的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]; }