我正在使用Urban Airship (v3) API将消息发送到Android / iPhone / Blackberry,希望很快能推出Windows手机。我对此不负责任;相反,我正在设置后端以允许用户发送广播。
另一个人构建了原始后端,但我选择从下往上重建它以添加一些额外的功能。一切都在其中工作,除了整个广播部分的推动。好吧,它有点工作;让我解释一下:
当提交表单时,数据通过MYSQL进入数据库,然后使用mysql_fetch_id()我获取新的id并将该id转换为名为sentBroadcast的PHP函数。它看起来如下:
function sentBroadcast($id){
$alertinfo = getAlertInfo($id);//this just gets all the data matching the id
$alert = mysql_fetch_assoc($alertinfo);
//these just get extra values
$organization = getOrganizationById($alert['broadcast_organization_id']);
$cityinfo = getCityInfo($organization['organization_city_id']);
$city = mysql_fetch_assoc($cityinfo);
// Create Airship object
$airship = new Airship(APP_KEY, APP_MASTER_SECRET);
$apiurl = "https://go.urbanairship.com/api/location/?q=".str_replace(" ","",strtolower($city['city_name']));
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $apiurl);
curl_setopt($ch, CURLOPT_USERPWD, APP_KEY.":".APP_MASTER_SECRET);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$output = curl_exec($ch);
curl_close ($ch);
$json = json_decode($output);
$locationid = "all";
if(count($json->features) > 0){
$locationid = $json->features[0]->id;
}
//send the message
$broadcasttype = "";
if($alert['broadcast_broadcasttypeother'] != ""){
$broadcasttype = $alert['broadcast_broadcasttypeother'];
}
else {
//this just gets data, nothing to see here
$broadcasttype = getCategoryInfo($alert['broadcast_broadcasttype_id'],'broadcasttype_name');
}
$message = html_entity_decode($broadcasttype)."\r\n".html_entity_decode($organization['organization_name'])."\r\n". html_entity_decode($alert['broadcast_subject']);
$blackberry_message = html_entity_decode($organization['organization_name'])."\r\n". html_entity_decode($alert['broadcast_subject']);
//calc as UTC
$timestamp = strtotime($alert['broadcast_sentdatetime']) + strtotime("+1 minute"); //add an hour
$offset = new DateTime(date("Y-m-d H:i:s T",$timestamp));
$offset->setTimezone(new DateTimeZone('UTC'));
$minutes_to_add = 10;
$time = new DateTime($alert['broadcast_sentdatetime']);
$time->add(new DateInterval('PT' . $minutes_to_add . 'S'));
$stamp = $time->format('Y-m-d H:i:s');
//echo $stamp;
$broadcast_message = array(
'schedule' => array("scheduled_time" => $stamp),
'push' => array("audience" => "all",
"notification" => array("alert" => $message),
"device_types" => array()
),
);
$device_types = array();
$device_types[] = "ios";
$device_types[] = "android";
$device_types[] = "blackberry";
$broadcast_message["push"]["device_types"] = $device_types;
if(in_array("ios", $device_types)){
$broadcast_message["push"]["notification"]["ios"] = array("sound" => "police.mp3", "extra" => array("id"=>$alert['broadcast_id']), "badge" => "+1");
}
if(in_array("android", $device_types)){
$broadcast_message["push"]["notification"]["android"] = array("extra"=>array("id"=>$alert['broadcast_id']));
}
if(in_array("blackberry", $device_types)){
$broadcast_message["push"]["notification"]["blackberry"] = array("content-type"=>"text/plain","body"=> json_encode(array("id"=>$alert['broadcast_id'], "body"=>$blackberry_message, "type"=>$broadcasttype)));
}
$data_string = json_encode($broadcast_message);
$apiurl = "https://go.urbanairship.com/api/schedules/";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $apiurl);
curl_setopt($ch, CURLOPT_USERPWD, APP_KEY.":".APP_MASTER_SECRET);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-type: application/json',
'Accept: application/vnd.urbanairship+json; version=3;',
'Content-Length: ' . strlen($data_string)
));
$json = curl_exec($ch);
$output = json_decode($json, true);
if(!is_array($output) || empty($output["ok"])){
echo "<h1>ERROR: (".(isset($output["error"]) ? $output["error"] : "An unknown error has occurred while trying to send your message.").")</h1>";
echo $data_string;
print_r($output);
$error = true;
}
curl_close ($ch);
$debug = false;
if($debug || $error){
if($error) echo "<!--";
var_dump($broadcast_message);
echo "<hr><br>";
echo $json."<hr><br>";
echo "<pre>";
print_r($output);
echo "</pre>";
if(!empty($output['ok'])){
//maybe we should save the status, or the json in the db.
echo 'yay it sent';
}
if($error) echo "-->";
}
if($error){
exit;
}
}//end sendBroadcast
当我执行此查询时,我遇到错误“无法解析正文请求正文”。这不是很有帮助,所以我打印了响应(看看“if(!is_array($ output)|| empty($ output [”ok“])){”)。我收到以下错误消息:
Array ( [ok] => [error] => Could not parse request body. [error_code] => 40700 [details] => Array ( [error] => Cannot schedule for the past 2013-10-12T06:46:00.000Z ) [operation_id] => 6fde4fa0-4b64-11e3-8903-90e2ba0253a0 )
我得到的错误是“无法安排过去”,但在提交时,这是未来。我开始做一些研究并读到我必须将其设置为UTC时间。话虽这么说,无论我现在的时间如何,它在UTC中总是过去6小时,所以我必须将其转换为UTC。
所以,我这样做了,消息消失了,手机收到了消息,一切顺利。除非我们去阅读消息:然后我们收到一条错误消息,表示消息已被删除。
我们没有删除它,所以我想也许(尚未用了6个小时)用户手机将来会收到新的广播,但他们现在收到通知。该警报尚未显示,因此会引发错误。至少这就是我的想法;它还没有6个小时,所以我无法证明这一点。
我的问题是这样的:我如何告诉Urban Airship我想要立即发布,而不必在当前时间增加6个小时以使其成为“现在”,以及实际拥有手机是否在正确的时间获得?
我联系了UA,但他们说“希望延迟一周回复你”(不急,呃?)然后我用Google搜索错误代码(40700)并且什么也没得到。然后我通过电子邮件发送了构建原始文件的人,他说的只是“UTC非常重要”。谢谢你。
如果有人能帮助我,我会非常感激。
谢谢。
哦,如果有人想知道,我提交的json看起来如下:
{"schedule":{"scheduled_time":"2013-10-12 06:46:00"},"push":{"audience":"all","notification":{"alert":"Message\r\nKenton Industries\r\nKenton Test","ios":{"sound":"police.mp3","extra":{"id":"406"},"badge":"+1"},"android":{"extra":{"id":"406"}},"blackberry":{"content-type":"text\/plain","body":"{\"id\":\"406\",\"body\":\"Kenton Industries\\r\\nKenton Test\",\"type\":\"Message\"}"}},"device_types":["ios","android","blackberry"]}}
谢谢:)
答案 0 :(得分:0)
我不确定我理解你的问题的第二部分,“让电话在正确的时间得到它”。关于“如何告诉Urban Airship我想要立即发布的问题,而不必在当前时间增加6小时以使其成为”现在“”:
如果您希望用户尽快收到消息,则不应安排消息。通过their curl examples,即时推送消息卷曲请求应该如此:
curl -v -X POST -u "<AppKey>:<MasterSecret>" -H "Content-type: application/json" -H "Accept: application/vnd.urbanairship+json; version=3;" --data '{"audience" : "all", "device_types" : "all", "notification" : {"alert": "This is a broadcast"} }' https://go.urbanairship.com/api/push/