本文介绍了APNS PHP JSON有效负载结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在通过PHP脚本发送推送通知以连接到APNS服务器.当我使用下面的有效负载结构时,一切工作正常.

I am sending push notifications via a PHP script to connect to APNS server. Everything is working fine when I use the below payload structure.

$body['aps'] = array(
    'alert' => $message,
    'badge' => $badge,
    'sound' => 'default'

    );

$payload = json_encode($body);

但是,我需要向'alert'元素添加更多参数,并希望添加更多自定义参数.我的方法如下,但是APNS不接受JSON.我的PHP中的JSON创建方法有问题吗?

However, I need to add more parameters to the 'alert' element as well as want to add some more custom parameters. The way I do is is as follows, but APNS is not accepting the JSON. Is it a problem with my JSON creation method in PHP?

$payload='{

"aps": {
    "alert":{
    "title": "'.$message.'",
    "body":"'.$notif_desc.'"
            },
"badge":"'.$badge.'",
"sound": "default"


},
"type": "notification",
"id":"'.$lastid.'",
"date:"'.$date1.'"

}';

所以基本上,我有两个查询.第二种方法是错误的吗?如果是这样,请告诉我一个有效的方法来为APNS服务器创建嵌套的JSON有效负载.第二个问题,我需要向有效负载中添加自定义PHP变量,我想知道在第二种方法中添加它的方式是对还是错.

So basically, I have two queries. IS the second method wrong? If so, please show me a valid method to create nested JSON Payload for APNS server. Second question, I need to add custom PHP variables to the Payload, I want to know whether the way I have added it in the second method is right or wrong.

基本上,我需要在PHP中如下创建一个JSON对象

basically, I need to create a JSON object as below in PHP

{
    "aps" : {
        "alert" : {
            "title" : "Game Request",
            "body" : "Bob wants to play poker",
            "action-loc-key" : "PLAY"
        },
        "badge" : 5,
    },
    "acme1" : "bar",
    "acme2" : [ "bang",  "whiz" ]
}

推荐答案

在日期"属性后您缺少双引号:

You're missing a double quote after the "date" property:

"date:"'.$date1.'"

...应该是...

... should be...

"date":"'.$date1.'"

我建议首先将有效负载作为PHP对象/数组放在一起(就像您的原始示例一样),因为这样可以更容易地看到这种格式的结构,而不是巨大的串联字符串.例如

I'd recommend putting the payload together as a PHP object/array first (like your original example) as it is much easier to see the structure in that format rather than a giant concatenated string. E.g.

$payload['aps'] = array(
    'alert' => array(
        'title' => $title,
        'body' => $body,
        'action-loc-key' => 'PLAY'
    ),
    'badge' => $badge,
    'sound' => 'default'
);
$payload['acme1'] = 'bar';
$payload['acme2'] = array(
    'bang',
    'whiz'
);

$payload = json_encode($body);

这篇关于APNS PHP JSON有效负载结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-29 10:52
查看更多