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

查看:490
本文介绍了当用户点击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 应用程序的 launchOptions 参数中:didFinishLaunchingWithOptions:在<$ c $中c> AppDelegate 以下是执行此操作的步骤:

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 :您的通知字符串,标识符: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 选择它并更改以下值

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


  1. 使您的推送通知发送故事板ID在有效负载中,密钥标识符

  1. Make your Push Notification send the storyboard ID in its payload with the key identifier



  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
}

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

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