问题
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);
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.'"
}';
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.
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" ]
}
回答1:
You're missing a double quote after the "date" property:
"date:"'.$date1.'"
... should be...
"date":"'.$date1.'"
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);
回答2:
$send_data =
array(
'aps' =>
array
(
'status_code'=>200,
'alert'=>$message,
'sound' => 'default',
'notification_type'=>'DoctorPendingRequest',
'request_id'=>$request_id
)
);
Passs $send_data to payload.
来源:https://stackoverflow.com/questions/33212049/apns-php-json-payload-structure