jQuery と jsSHA(https://caligatio.github.io/jsSHA/) を利用して、
JavaScriptから Nifty Cloud mb の REST API を呼び出す。
参考 REST API リファレンス
http://mb.cloud.nifty.com/doc/current/rest/common/format.html
/**
* RESTAPI /2013-09-01/installations に PUSH通知用キーを登録する
*/
postInstallation: function (deviceToken) {
var self = this, str = "", signature = "",
now = (new Date()).toISOString(),
// アプリケーションキー
applicationKey = '0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef',
// クライアントキー
clientKey = '0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef',
params =
'SignatureMethod=HmacSHA256&SignatureVersion=2'
+ '&X-NCMB-Application-Key=' + applicationKey
+ '&X-NCMB-Timestamp=' + now
// URLにクエリパラメータがある場合、付与
// + '&where=%7B%22testKey%22%3A%22testValue%22%7D';
;
str += "POST\n";
str += "mb.api.cloud.nifty.com\n";
str += "/2013-09-01/installations\n";
str += params;
// HMAC-SHA256
var sha256 = new jsSHA('SHA-256', 'TEXT');
sha256.setHMACKey(clientKey, 'TEXT');
sha256.update(str);
signature = sha256.getHMAC("B64");
$.ajax({
url: 'https://mb.api.cloud.nifty.com/2013-09-01/installations',
type: 'POST',
headers: {
'X-NCMB-Application-Key': applicationKey,
'X-NCMB-Timestamp': now,
'X-NCMB-Signature': signature,
"Content-Type": "application/json",
},
dataType: 'json',
data: JSON.stringify({
"deviceType": "ios",
"deviceToken": deviceToken,
}),
}).then(function (data) {
console.log(data);
});
}