当用户点击iOS Swift的推送通知时,在特定视图中打开应用程序
我的应用允许向用户发送远程推送通知。当用户点击推送通知时,如何在特定视图控制器中打开它?我希望应用程序打开并导航到特定的视图控制器,具体取决于收到的推送通知。
My app allows remote push notifications to a user. How do I enable it to be opened in a specific view controller when the user taps on the push notification? I want the app to open and navigate to a specific view controller depending on the push notification received.
要做到这一点,你需要为您的应用程序可能打开的每个 ViewController
设置标识符
,然后检查 payload
在应用程序的
在 AppDelegate 以下是执行此操作的步骤: launchOptions
参数中:didFinishLaunchingWithOptions:
To do this you need to set an identifier
for each ViewController
that your app may be opened with, and then check the payload
in the launchOptions
argument of application:didFinishLaunchingWithOptions:
in your AppDelegate
Here are the steps to doing this:
-
在
PFPush
,使用setData
使用标识符为您的有效负载添加密钥:notification.setData([alert :您的通知字符串,标识符:firstController])
In your
PFPush
, usesetData
to add a key to your payload with the identifier:notification.setData(["alert":"your notification string", "identifier":"firstController"])
设置标识符
每个 ViewController
选择它并更改以下值
Set the identifier
on each ViewController
by selecting it and changing the following values
- 使您的推送通知发送故事板ID在
有效负载
中,密钥标识符
- Make your Push Notification send the storyboard ID in its
payload
with the keyidentifier
- 检查应用程序中的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
}