2013-08-25 104 views
0

我使用这个代码用发送设备到设备与urbanairship Android和PHP

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包含无效的设备令牌” ERROR

设备是否令牌需要以特定的JSON格式发送?我找不到此方法的任何文档。我是从this question

在此先感谢

回答

0

好吧,我得到它的工作

的正确格式为here

需要改变顺序和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); 
+0

http://docs.urbanairship.com/connect/android_push.html#push是一个断开的链接。 –