将php数组设备令牌发送到Urban Airship推送

时间:2013-11-01 20:50:29

标签: php curl notifications urbanairship.com

我很傻。我正在尝试使用城市飞艇发送到设备ID的php阵列。我正在使用找到here的第一个示例。一切正常,"audience"=>"all"。每个注册的设备都会被点击我需要查询一个数据库,其中包含一堆设备ID,然后发送到那些设备ID。我如何更改“观众”=>“全部”,以便我可以做到这一点。我已经尝试了一切!

以下是链接中断的代码:

<?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);
?>

3 个答案:

答案 0 :(得分: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"
}

答案 1 :(得分: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/

答案 2 :(得分:0)

切换到用于Urban Airship的PHP 2库,我能够发送到单个设备令牌。我还能够从数组中读取标记,并将数组值指定为目标。版本2找到here