0

我正在使用此代码

define('APPKEY','XXXXXXXXXXXXXX'); 
define('PUSHSECRET', 'XXXXXXXXXXXXX '); // Master Secret
define('PUSHURL', 'https://go.urbanairship.com/api/push/broadcast/'); 


$msg = "This is a message intended for my iPad 3";

$devicetokens = array();
$devicetokens[0] = $devicetoken;

$contents = array(); 
$contents['badge'] = "1"; 
$contents['alert'] = $msg; 
$contents['sound'] = "default"; 

$push = array("aps" => $contents, "device_tokens" =>$devicetokens); 


$json = json_encode($push); 
$session = curl_init(PUSHURL); 
curl_setopt($session, CURLOPT_USERPWD, APPKEY . ':' . PUSHSECRET); 
curl_setopt($session, CURLOPT_POST, True); 
curl_setopt($session, CURLOPT_POSTFIELDS, $json); 
curl_setopt($session, CURLOPT_HEADER, False); 
curl_setopt($session, CURLOPT_RETURNTRANSFER, True); 
curl_setopt($session, CURLOPT_HTTPHEADER, array('Content-Type:application/json')); 
$content = curl_exec($session); 

var_dump($content); // just for testing what was sent

 // Check if any error occured 
$response = curl_getinfo($session);

但什么也没发生 - 我得到“device_token contains an invalid device token”错误

设备令牌是否需要以特定的 JSON 格式发送?我找不到此方法的任何文档。我从这个问题中得到了

提前致谢

4

1 回答 1

0

好的,我开始工作了

正确的格式在这里

需要更改 JSON 对象和属性的顺序和名称

$msg = "This is a message intended for Nexus";

$devicetokens    = array(); 
$devicetoken     = "YOUR DEVICE TOKEN"; 
$devicetokens[0] = $devicetoken;

$contents = array(); 
$contents['alert'] = $msg; 
$contents['sound'] = "default";

$push = array("apids" =>$devicetokens, "android" => $contents);

$json = json_encode($push);
于 2013-08-26T01:28:20.483 回答