【发布时间】:2025-12-14 06:20:22
【问题描述】:
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject])
{
PFPush.handlePush(userInfo)
if application.applicationState == UIApplicationState.Active
{
PFAnalytics.trackAppOpenedWithRemoteNotificationPayload(userInfo)
if let msg = userInfo["test"] as? Dictionary<String,AnyObject>
{
print(msg)
}
}
我的 userInfo["test"] JSON 值为:
> Printing description of userInfo:
▿ 3 elements
▿ [0] : 2 elements
- .0 : message
- .1 : 1235
▿ [1] : 2 elements
- .0 : aps
- .1 : 0 elements
▿ [2] : 2 elements
- .0 : link
- .1 :
我可以获取我的 userInfo 数据,但我不知道为什么我的 msg 没有任何价值(甚至没有 nil 只是没有可显示的内容) 我怎么才能到 print(msg) ?
更新
这是我的推送:
$this->parsepush->send(array("channels" => ['test'], "data" => > $post_data));
在 $post_data 示例中:
$post_data = json_encode(数组 ('link' => $link, 'message' => $message));\
我已经尝试过来自的指令,但我也无法进行简单的打印。
编辑
仍然跳过了“aps”部分。
我的 JSON
这是我的Json 的样子:
> {
"aps":{
"alert":{
"title":"$title",
"body":"$message"
},
"badge":"1"
},
"adira":{
"link":"$link"
}
}
这是我的代码:
> func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) {
// PFPush.handlePush(userInfo)
// PFAnalytics.trackAppOpenedWithRemoteNotificationPayload(userInfo)
if let msg = userInfo["message"] as? String {
print(msg)
print("a")
}
if let alertDict = userInfo["aps"]?["alert"] as? Dictionary<String, String> {
print("title : ", alertDict["title"])
print("body :", alertDict["body"]!)
print("badge: ", alertDict["badge"]!)
print("b")
}
if let link = userInfo["link"] as? String {
print(link)
print("c")
}
}
}
但我仍然无法获取我的aps 数据。仍然为零或一无所有。
如果你想要一个特定的代码,只需点击下面的链接:
编辑 2
我直接使用 print(userInfo) 打印输出是:
对象已保存。 [消息:iOS推送,aps:{},链接:]
更新 2
我尝试从 parse.com 推送,而不是我的网站(第 3 方)。 这就是我从普通打印(userInfo)中得到的
> [aps: {
alert = 12345abc;
sound = default;
}]
所以我想我会更改并在我的 JSON 中添加一些内容从我的网站到:
[aps:{
alert = $message;
sound = default;
link = $link;
}]
类似的东西?
【问题讨论】:
-
好吧,
test键没有任何价值,只有message、aps和link。请附上您发送的内容。 -
@jcaron 这里是我从网站
$this->parsepush->send(array( "channels" => ['test'], "data" => $post_data ));推送的内容所以我可以将 JSON 转换为 Dictionary 吗?还是字符串? -
不要在 cmets 中添加代码,编辑您的问题以添加代码。此外,您实际上没有包括您发送的内容 (
$post_data)。你收到的 JSON 已经被 iOS 解码了,但是你还没有告诉我们你发送的 JSON 是什么。 -
好的,抱歉,我会修复它。 @jcaron
-
这是更新,该功能是使用 parse.com 监听来自我的网络服务的推送通知。我的网络服务使用 JSON 格式来推送它的数据,使用 2 个元素:(“消息”和“链接”),链接可以为空。 userInfo 已经正确接受了打印的数据,现在我需要提取消息数据(例如:“1235”,就像上面的数据一样),所以我可以使用它来创建一个带有该数据标题的通知(“1235”)我我的推送也更新了@jcaron