私は、Urban Airship (v3) APIを使用して、メッセージを Android/iPhone/Blackberry にプッシュし、できればすぐに Windows Phone にプッシュします。私はそれについて責任を負いません。代わりに、ユーザーがブロードキャストを送信できるようにバックエンドをセットアップしています。
別の人が元のバックエンドを構築しましたが、私はそれをボトムアップで再構築して、いくつかの追加機能を追加することにしました。ブロードキャスト部分のプッシュ全体を除いて、すべてが機能します。まあ、それはうまくいきます。説明させてください:
フォームが送信されると、データは MYSQL 経由でデータベースに入り、mysql_fetch_id() を使用して新しい ID を取得し、その ID を sendBroadcast という 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時間経っていないので、それを証明することはできません.
私の問題は次のとおりです。「現在」にするために現在の時刻に6時間を追加する必要がなく、実際に電話でそれを取得することなく、すぐに投稿したいことをUrban Airshipに伝えるにはどうすればよいですか正しい時間?
UA に連絡したところ、「返信が 1 週間遅れる見込みです」とのことでした (急ぐ必要はありません)。それから、オリジナルを作った人にメールを送りましたが、彼は「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"]}}
ありがとう :)