我要处理一些更多的信息,从我有效载荷。这是 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,所以我的应用程序可以重定向到不同的部分或推荐的外部链接。(它不总是一个 url) 我试着︰ 让 my_url = ap ["link_url"] 作为吗?NSDictionary,然后描述作为字符串,但是我什么也没有。
它取决于如何定义 $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"
}
}