2013-11-01 35 views
1

我很烂。我正尝试发送一个带有城市飞艇的设备ID的php数组。我使用的第一个例子是here。一切正常,"audience"=>"all"。每个注册的设备都会被击中我需要查询一个数据库,它有一堆设备ID,并发送到这些设备ID。我该如何改变“观众”=>“全部”,以便我可以做到这一点。我已经尝试了一切!发送设备令牌的PHP数组到城市飞艇推

这里是柜面的链接断码:

<?php 
define('APPKEY','XXXXXXXXXXXXXXX'); // Your App Key 
define('PUSHSECRET', 'XXXXXXXXXXXXXXX'); // Your Master Secret 
define('PUSHURL', 'https://go.urbanairship.com/api/push/'); 

$contents = array(); 
$contents['badge'] = "+1"; 
$contents['alert'] = "PHP script test"; 
$contents['sound'] = "cat.caf"; 
$notification = array(); 
$notification['ios'] = $contents; 
$platform = array(); 
array_push($platform, "ios"); 

$push = array("audience"=>"all", "notification"=>$notification, "device_types"=>$platform); 

$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', 'Accept: application/vnd.urbanairship+json; version=3;')); 
$content = curl_exec($session); 
echo $content; // just for testing what was sent 

// Check if any error occured 
$response = curl_getinfo($session); 
if($response['http_code'] != 202) { 
    echo "Got negative response from server, http code: ". 
    $response['http_code'] . "\n"; 
} else { 

    echo "Wow, it worked!\n"; 
} 

curl_close($session); 
?> 

回答

0

切换到PHP 2库城市飞艇,我是能够发送到单个设备令牌。我也能够从数组中读取令牌,并将数组值赋值为目标。版本2发现here

1

这取决于什么设备的操作系统,你要发送到。通过他们的文档在这里:

http://docs.urbanairship.com/reference/api/v3/push.html#atomic-selectors

您需要设置正确的设备类型,它对应的ID。例如:

安卓

"audience" : { 
    "apid" : "b8f9b663-0a3b-cf45-587a-be880946e880" 
} 

IOS:

"audience" : { 
    "device_token" : "C9E454F6105B0F442CABD48CB678E9A230C9A141F83CF4CC03665375EB78AD3A" 
} 
+0

无论出于何种原因,该格式也不起作用。我终于使用了Urban Airship的PHP 2图书馆。它有很多额外的依赖性来安装,但是马上就可以工作。谢谢! – Siriss

1

我从城市飞艇帮助中心找到了一个可能的解决方案...他们提出这个建议。它为我工作。

您可以在单个请求中发送到多个设备令牌或APID。我会建议使用我们的新API v3并加强您的请求。有几个方法可以做到这一点:

1)发送到多个设备的一个有效载荷

curl -v -X POST -u "<AppKey>:<MasterSecret>" -H "Content-type: application/json" -H "Accept: application/vnd.urbanairship+json; version=3;" --data '{"audience" : {"OR": [{"device_token":"<DeviceToken1>"}, {"device_token":"<DeviceToken2>"}, {"device_token":"<DeviceToken3>"}]}, "notification" : {"alert" : "Hello iOS devices!"}, "device_types" : ["ios"]}' https://go.urbanairship.com/api/push/

OR

2)将多个有效载荷一起在一个批次

curl -v -X POST -u "<AppKey>:<MasterSecret>" -H "Content-type: application/json" -H "Accept: application/vnd.urbanairship+json; version=3;" --data '[{"audience": {"device_token": "<DeviceToken1>"}, "notification": {"alert": "Hello, I was sent along with a batch of other pushes!"}, "device_types": ["ios"]}, {"audience": {"device_token": "<DeviceToken2>"}, "notification": {"alert": "I was also sent with a batch of other pushes!"}, "device_types": ["ios"]}, {"audience": {"device_token": "<DeviceToken3>"}, "notification": {"alert": "Me three!"}, "device_types": ["ios"]}]' https://go.urbanairship.com/api/push/