【问题标题】:Open app in specific view when user taps on push notification with iOS Swift当用户使用 iOS Swift 点击推送通知时,在特定视图中打开应用
【发布时间】:2016-02-15 20:15:28
【问题描述】:

我的应用允许向用户远程推送通知。当用户点击推送通知时,如何在特定的视图控制器中打开它?我希望应用根据收到的推送通知打开并导航到特定的视图控制器。

【问题讨论】:

    标签: ios iphone swift push-notification


    【解决方案1】:

    在 AppDelegate 中,您将获得委托回调“didFinishLoading”或“didReceivePushNotification”方法(基于您的应用在后台或前台)。在该方法中获取最顶层视图控制器的实例,然后创建您要显示的特定视图控制器并从最顶层视图控制器呈现/推送。

    【讨论】:

    • Satyam,感谢您的回答,这也是正确的。但我给了 kabiroberai 的答案打勾,因为他是我问题的最完整的解决方案。
    • @Satyam,我们怎么知道我们需要根据推送通知导航到哪个控制器?请澄清这一点。
    • @NarasimhaNallamsetty,您的推送通知负载必须包含必要的信息。可能是您的服务器人员必须向有效负载添加更多信息。访问Apple website。根据额外的信息,您可以确定您必须导航哪个视图控制器。
    【解决方案2】:

    为此,您需要为可以打开您的应用程序的每个ViewController 设置一个identifier,然后在AppDelegate 中的application:didFinishLaunchingWithOptions:launchOptions 参数中检查payload执行此操作的步骤是:

    1. 在您的PFPush 中,使用setData 将密钥添加到您的有效负载中,其标识符为:notification.setData(["alert":"your notification string", "identifier":"firstController"])

    2. 通过选择每个ViewController 并更改以下值来设置identifier

    1. 让您的推送通知在其payload 中发送情节提要ID,并使用密钥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
    }
    

    【讨论】:

    • 我正在使用 PFPush,目前,我的推送通知仅包含一串文本
    • @mechdon 使用方法 setData 在您的推送通知中使用字典 ["identifier":"firstController"] 或您的标识符应该是什么来发送数据和推送
    • @mechdon 你打算如何指定ViewController 打开?
    • 我查阅了 PFPush 类参考。它说如果我使用 setMessage,这将覆盖 setData 中指定的任何数据。除了标识符,我还需要设置消息。
    • @mechdon 在您的字典中添加一个名为 alert 的键并将其值设置为您要发送的警报,如下所示:["alert":"your notification string", "identifier":"firstController"]
    【解决方案3】:
     UILocalNotification *notification = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
        if (notification)
        {
            [self application:application didReceiveRemoteNotification:(NSDictionary*)notification];
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多