php - Urban Airship API 错误代码 40700

标签 php mysql curl urbanairship.com

我正在与 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 )

我收到的错误是“无法安排过去”,但是在提交此错误时,这是​​ future 。我开始做一些研究并了解到我必须将其设置为 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"]}}

谢谢:)

最佳答案

我不确定我是否理解您问题的第二部分,“让手机在正确的时间收到它”。关于“我如何告诉 Urban Airship 我想要立即发布帖子,而不必在当前时间上添加 6 小时以使其处于“当前”状态”的问题:

如果您希望用户尽快收到消息,那么您不应该安排消息。通过their curl examples立即推送消息 curl 请求应如下所示:

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/ 

关于php - Urban Airship API 错误代码 40700,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19923539/

相关文章:

java - 在java中单个语句中执行多个查询

PHP 在循环中使用 cURL 的正确方法?

php - MySQL/PHP 清理/验证存储在数据库中的 URL 的最佳方法

PHP AltoRouter 仅提供基本 URL

php - 检查有效 token 以更新密码

MySQL查询优化

PHP PDO 按列名分组查询结果

php - 即使在 php 中设置时区后,服务器中的时间戳也会给出错误的时间

node.js - OpenShift Gears 返回不同的 cURL 结果

c - 压测服务器: starting requests at given subsecond timestamps