当用户点击带有 iOS Swift 的推送通知时,在特定视图中打开应用程序
Open app in specific view when user taps on push notification with iOS Swift
我的应用允许向用户远程推送通知。当用户点击推送通知时,如何让它在特定的视图控制器中打开?我希望应用程序根据收到的推送通知打开并导航到特定的视图控制器。
在 AppDelegate 中,您将获得委托回调 "didFinishLoading" 或 "didReceivePushNotification" 方法(取决于您的应用是在后台还是前台)。在该方法中获取最顶层视图控制器的实例,然后创建要显示的特定视图控制器,并从最顶层视图控制器创建 present/push。
为此,您需要为您的应用程序可能打开的每个 ViewController
设置一个 identifier
,然后检查 launchOptions
参数中的 payload
AppDelegate
中的 application:didFinishLaunchingWithOptions:
以下是执行此操作的步骤:
在您的 PFPush
中,使用 setData
向您的负载添加一个密钥,标识符为:notification.setData(["alert":"your notification string", "identifier":"firstController"])
通过选择每个 ViewController
并更改以下值来设置 identifier
- 让您的推送通知在其
payload
中使用密钥 identifier
发送故事板 ID
- 检查application:didFinishLaunchingWithOptions中的ID:在函数末尾添加以下内容:
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
}
UILocalNotification *notification = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
if (notification)
{
[self application:application didReceiveRemoteNotification:(NSDictionary*)notification];
}
我的应用允许向用户远程推送通知。当用户点击推送通知时,如何让它在特定的视图控制器中打开?我希望应用程序根据收到的推送通知打开并导航到特定的视图控制器。
在 AppDelegate 中,您将获得委托回调 "didFinishLoading" 或 "didReceivePushNotification" 方法(取决于您的应用是在后台还是前台)。在该方法中获取最顶层视图控制器的实例,然后创建要显示的特定视图控制器,并从最顶层视图控制器创建 present/push。
为此,您需要为您的应用程序可能打开的每个 ViewController
设置一个 identifier
,然后检查 launchOptions
参数中的 payload
AppDelegate
中的 application:didFinishLaunchingWithOptions:
以下是执行此操作的步骤:
在您的
PFPush
中,使用setData
向您的负载添加一个密钥,标识符为:notification.setData(["alert":"your notification string", "identifier":"firstController"])
通过选择每个
ViewController
并更改以下值来设置identifier
- 让您的推送通知在其
payload
中使用密钥identifier
发送故事板 ID
- 检查application:didFinishLaunchingWithOptions中的ID:在函数末尾添加以下内容:
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
}
UILocalNotification *notification = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
if (notification)
{
[self application:application didReceiveRemoteNotification:(NSDictionary*)notification];
}