1

我很烂。我正在尝试使用城市飞艇发送到设备 ID 的 php 数组。我正在使用此处找到的第一个示例。一切正常,与"audience"=>"all". 每个注册的设备都会受到打击。我需要查询一个包含一堆设备 ID 的数据库,然后发送到这些设备 ID。我应该将“audience”=>“all”更改为什么,这样我才能做到这一点。我已经尝试了一切!

这是链接中断的代码:

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

3 回答 3

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"
}
于 2013-11-04T19:35:10.143 回答
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/

或者

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/

于 2014-01-25T11:58:36.287 回答
0

切换到 Urban Airship 的 PHP 2 库,我能够发送到单个设备令牌。我还能够从数组中读取标记,并将数组值分配为目标。版本 2 在这里找到。

于 2013-12-09T19:57:27.363 回答