【问题标题】:iPhone Push Payload Info in AppiPhone 在 App 中推送有效载荷信息
【发布时间】:2016-12-15 18:09:13
【问题描述】:

我正在尝试从我的有效负载中处理更多信息。这就是我为 iPhone 设置推送通知和消息通过的方式,这就是我创建有效负载的方式:

// Create the payload body
$body['aps'] = array(
  'alert' => $message,
  'sound' => 'default',
  'link_url' => $url,
  );

这是在我的应用程序中。

// WHEN a push notification comes in
    func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any]){
        if let aps = userInfo["aps"] as? NSDictionary {
            if let alert = aps["alert"] as? NSDictionary {
                if (alert["message"] as? NSString) != nil {
                    //Do stuff
                    self.window = UIWindow(frame: UIScreen.main.bounds)
                    let storyboard = UIStoryboard(name: "Main", bundle: nil)

                    // redirect to section
                    var initialViewController = storyboard.instantiateViewController(withIdentifier: "ViewBookings")
                    self.window?.rootViewController = initialViewController
                    self.window?.makeKeyAndVisible()
                }
            } else if (aps["alert"] as? NSString) != nil {
                self.window = UIWindow(frame: UIScreen.main.bounds)
                let storyboard = UIStoryboard(name: "Main", bundle: nil)

                // redirect to section
                var initialViewController2 = storyboard.instantiateViewController(withIdentifier: "ViewBookings")
                self.window?.rootViewController = initialViewController2
                self.window?.makeKeyAndVisible()
            }
        }
    }

但现在我需要阅读 link_url,以便我的应用可以重定向到不同的部分或推荐外部链接。 (它并不总是一个网址) 我试过:让 my_url = aps["link_url"] as? NSDictionary 然后描述为字符串,但我什么也没得到。

【问题讨论】:

  • 你的 aps["link_url"] 是字典还是字符串?

标签: iphone swift xcode push-notification


【解决方案1】:

这取决于 $message 和 $url 对象是如何定义的。如果它们是字符串,那么以下应该可以工作:

if let my_url = aps["link_url"] as? String {
    print(my_url)
}

从您的代码看来,您的对象如下所示:

{ "aps" : {
    "alert" : {
        "message" : "some message"
    },
    "sound" : "default",
    "link_url" : "www.example.com"
    }
}

【讨论】:

    猜你喜欢
    • 2021-04-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-01
    相关资源
    最近更新 更多