Last active
March 2, 2017 04:33
-
-
Save kuluna/fa5d48e1cc4ccb59a2f93553639d512c to your computer and use it in GitHub Desktop.
Push notification used Nifty Mobile Backend
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
var req = require("request"); | |
var crypto = require("crypto"); | |
exports.push = function(niftyAppKey, niftyClientKey, title, message) { | |
const uri = "https://mb.api.cloud.nifty.com/2013-09-01/push"; | |
const timestamp = new Date().toISOString(); | |
const headers = { | |
"X-NCMB-Application-Key": niftyAppKey, | |
"X-NCMB-Signature": exports.generateSignature(niftyAppKey, niftyClientKey, uri, "POST", timestamp), | |
"X-NCMB-Timestamp": timestamp, | |
"Content-Type": "application/json" | |
}; | |
const body = { | |
immediateDeliveryFlag: true, | |
target: ["android"], | |
title: title, | |
message: message | |
}; | |
req.post(uri, {headers: headers, json: true, body: body}, function(error, response, body) { | |
if (error) { | |
console.warn(error); | |
} | |
return body; | |
}); | |
}; | |
// source: http://mb.cloud.nifty.com/doc/current/rest/common/signature.html | |
exports.generateSignature = function(applicationKey, clientKey, url, method, timestamp) { | |
var _applicationKey = applicationKey; | |
var _timestamp = timestamp; | |
var _clientKey = clientKey; | |
var _method = method; | |
var _url = encodeURI(url); | |
var _tmp = _url.substring(_url.lastIndexOf("//") + 2); | |
var _fqdn = _tmp.substring(0, _tmp.indexOf("/")); | |
var _position = _url.indexOf("?"); | |
var _path = ""; | |
var _data = {}; | |
if(_position == -1) { | |
_path = _url.substring(_url.lastIndexOf(_fqdn) + _fqdn.length); | |
} | |
else { | |
var _get_parameter= _url.substring(_position + 1).replace(/:/g , "%3A"); | |
_path = _url.substring(_url.lastIndexOf(_fqdn) + _fqdn.length, _position); | |
_tmp = _get_parameter.split("&"); | |
for (var i = 0; i < _tmp.length; i++) { | |
_position = _tmp[i].indexOf("="); | |
_data[_tmp[i].substring(0 , _position)] = _tmp[i].substring(_position + 1); | |
} | |
} | |
_data["SignatureMethod"] = "HmacSHA256"; | |
_data["SignatureVersion"] = "2"; | |
_data["X-NCMB-Application-Key"] = _applicationKey; | |
_data["X-NCMB-Timestamp"] = _timestamp; | |
var _sorted_data = {}; | |
var keys = []; | |
var k, i, len; | |
for (k in _data) | |
{ | |
if (_data.hasOwnProperty(k)) | |
{ | |
keys.push(k); | |
} | |
} | |
keys.sort(); | |
len = keys.length; | |
for (i = 0; i < len; i++) | |
{ | |
k = keys[i]; | |
_sorted_data[k] = _data[k]; | |
} | |
var parameterString = ""; | |
for (k in _sorted_data) | |
{ | |
if (_sorted_data.hasOwnProperty(k)) | |
{ | |
if (parameterString != "") { | |
parameterString += "&"; | |
}; | |
parameterString = parameterString + k + "=" + _sorted_data[k]; | |
} | |
} | |
var forEncodeString = _method + "\n" + _fqdn + "\n" + _path + "\n" + parameterString; | |
console.log(forEncodeString); | |
var hash = crypto.createHmac("sha256", _clientKey).update(forEncodeString).digest(); | |
var signature = hash.toString("base64"); | |
return signature; | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var nifty = require("./nmb"); | |
const appKey = "Nifty Cloud API application key"; | |
const clientKey = "Nifty Cloud API client key"; | |
nifty.push(appKey, clientKey, "push test", "body text") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment