gpt4 book ai didi

iPhone 在应用程序中推送有效负载信息

转载 作者:行者123 更新时间:2023-11-30 12:49:32 24 4
gpt4 key购买 nike

我正在尝试从我的有效负载中处理更多信息。这就是我为 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"] 作为? 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"
}
}

关于iPhone 在应用程序中推送有效负载信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41170683/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com