当用户使用 iOS Swift 点击推送通知时,在特定视图中打开应用程序 [英] Open app in specific view when user taps on push notification with iOS Swift

查看:18
本文介绍了当用户使用 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 设置一个 identifier,然后在你的 AppDelegate 中检查 application:didFinishLaunchingWithOptions:launchOptions 参数中的 payload 这里是步骤这样做:

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:

  1. 在您的 PFPush 中,使用 setData 将一个密钥添加到您的负载中,其标识符为:notification.setData(["alert":"您的通知字符串", "identifier":"firstController"])

  1. In your PFPush, use setData to add a key to your payload with the identifier: notification.setData(["alert":"your notification string", "identifier":"firstController"])

在每个 ViewController 上设置 identifier,方法是选择它并更改以下值

Set the identifier on each ViewController by selecting it and changing the following values

  1. 使您的推送通知在其 payload 中发送带有 identifier 键的故事板 ID
  1. Make your Push Notification send the storyboard ID in its payload with the key identifier

  1. 检查 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
}

这篇关于当用户使用 iOS Swift 点击推送通知时,在特定视图中打开应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆