mobilebackendでPUSH通知を利用していると、任意のユーザにPUSH通知を送りたい事があると思います。
その際に管理画面からobjectIdを1件ずつ指定するのは大変なので、対象者に検索用のフィールドを追加する処理をPHPでやってみました。
mobilebackendのAPIを利用する上の最難関たるシグネチャの生成処理は、下記のエントリをほぼまるごと流用させていただきました。
ニフティクラウド mobile backendの署名処理をPHPで実装してみた
<?php
date_default_timezone_set('UTC');
/*
* シグネチャを作成し返す、下記のプログラムを用いさせていただいています。
* ニフティクラウド mobile backendの署名処理をPHPで実装してみた
* http://qiita.com/niftycloud_mb/items/dee4558558e86175ccea
*
* $request_method : GET / POST / PUT
* $request_path : ドキュメントルートからのurl ex) /2013-09-01/installations
* $data_hash : POST / PUT時のパラメータクエリ
* $request_timestamp : ISO8601形式の時刻、リクエスト時のX-NCMB-Timestampヘッダと同値
*/
function getSignature($request_method,$request_path,$data_hash,$request_timestamp){
$method = $request_method;
$path = $request_path;
$hash = $data_hash;
$timestamp = $request_timestamp;
$header_string = "SignatureMethod=HmacSHA256&";
$header_string .= "SignatureVersion=2&";
$header_string .= "X-NCMB-Application-Key=".APPLICATION_KEY . "&";
$header_string .= "X-NCMB-Timestamp=".$timestamp;
if($method == "GET" && $hash != null){
$header_string .= "&" . http_build_query($hash);
}
$signature_string = $method . "\n";
$signature_string .= FQDN . "\n";
$signature_string .= $request_path ."\n";
$signature_string .= $header_string;
$signature = base64_encode(hash_hmac("sha256", $signature_string, CLIENT_KEY, true));
return $signature;
}
/*
* cUrlにて、mobilebackendAPIへリクエストを実施するクラス。結果をオブジェクトで返す
* $request_method : GET / POST / PUT
* $url : mobailebackendAPIのリクエストURL、標準クラス(userやinstallationなど)は専用のAPIがある
* $headers : X-NCMB-Application-Key / X-NCMB-Timestamp / X-NCMB-Signature / Content-Type
* $data_hash : POST / PUT時のパラメータクエリ
*/
function requestApi($request_method,$url,$headers,$data_hash){
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch,CURLOPT_HTTPHEADER, $headers);
switch($request_method){
case "GET":
curl_setopt($ch,CURLOPT_HTTPGET,true);
break;
case "POST":
curl_setopt($ch,CURLOPT_POST,true);
if($data_hash != null){
curl_setopt($ch,CURLOPT_POSTFIELDS,http_build_query($data_hash));
}
break;
case "PUT":
curl_setopt($ch,CURLOPT_CUSTOMREQUEST,"PUT");
if($data_hash != null){
curl_setopt($ch,CURLOPT_POSTFIELDS,$data_hash);
}
break;
}
$json = curl_exec($ch);
curl_close($ch);
$results = json_decode($json);
return $results;
}
# 固定値は定数にしておく。
define("APPLICATION_KEY","mobilebackendで作成したプロジェクトのアプリケーションキー");
define("CLIENT_KEY","mobilebackendで作成したプロジェクトのクライアントキー");
define("API_VERSION","2013-09-01"); // 多分固定値なので深く考えない
define("FQDN","mb.api.cloud.nifty.com"); // mobilebackendのリクエスト先API
# installationクラスから特定のobjectIdのリストを取得する。
# シグネチャの作成
$request_method = "GET";
$request_path = "/".API_VERSION."/installations";
# 'where={"objectId":{"$inArray":["objectIdの値その1","objectIdの値その2"]}}' というクエリを実施する
$data_hash['where'] = json_encode(array("objectId" => array('$inArray' => array("objectIdの値その1","objectIdの値その2"))));;
# 現在時刻をISO8601形式で取得
$t = new DateTime('NOW');
$t->setTimeZone( new DateTimeZone('UTC'));
$request_timestamp = $t->format('Y-m-d\TH:i:s\Z');
# シグネチャを取得
$signature = getSignature($request_method,$request_path,$data_hash,$request_timestamp);
# NCMP API へリクエストを行う準備
$headers = array(
"X-NCMB-Application-Key: ".APPLICATION_KEY,
"X-NCMB-Timestamp: ".$request_timestamp,
"X-NCMB-Signature: ".$signature,
"Content-Type: application/json"
);
# GETリクエストなので、パラメータクエリはURLに含める
$url = "https://".FQDN.$request_path."?".http_build_query($data_hash);
# cUrlにてAPIへリクエスト実施
$json_arr = requestApi($request_method,$url,$headers,$data_hash);
# 取得したinstallationクラスのレコード件数分ループする
foreach( $json_arr->results as $key => $val){
# データストアオブジェクト更新APIを用いして
# installationクラスに「field_1」フィールドを追加し、値:"1"を設定する。
# シグネチャ作成
$request_method = "PUT";
# データストアへのフィールドの操作はobjectId単位で行う。
$request_path = "/".API_VERSION."/installations/".$val->objectId;
# installationクラスのfield_1 の値を"1"にします、field_1が無ければ作成されます。
$data_hash = '{"field_1":"1"}';
# 現在時刻をISO8601形式で取得
$t = new DateTime('NOW');
$t->setTimeZone( new DateTimeZone('UTC'));
$request_timestamp = $t->format('Y-m-d\TH:i:s\Z');
# シグネチャを作成します。
$signature = getSignature($request_method,$request_path,$data_hash,$request_timestamp);
# APIに対してPUTリクエストを実施します。
$headers = array(
"X-NCMB-Application-Key: ".APPLICATION_KEY,
"X-NCMB-Timestamp: ".$request_timestamp,
"X-NCMB-Signature: ".$signature,
"Content-Type: application/json"
);
$url = "https://".FQDN.$request_path;
$json_result = requestApi($request_method,$url,$headers,$data_hash);
}