본문 바로가기

프로그래밍/nodeJS

nodeJS에서 GCM notification 구현하기


node-gcm npm은 nodejs에서 GCM notification push를 효과적으로 이루어지게 도와주는 모듈이다.


npm node-gcm 공식 사이트 : https://www.npmjs.com/package/node-gcm


node-gcm npm을 깔고 id와 apiKey를 등록하고, 안드로이드가 준비되어 있다면 아래와 같은 nodeJS코드를 활용하면 된다.

var gcm = require('node-gcm');
var message = new gcm.Message();
var message = new gcm.Message({
collapseKey: 'demo',
delayWhileIdle: true,
timeToLive: 3,
data: {
key1: 'demo title.',
key2: 'push demo'
}
});
var server_access_key = 'push provider access key value';
var sender = new gcm.Sender(server_access_key);
var registrationIds = [];
var registration_id = 'android registration_id value';
registrationIds.push(registration_id);
/**
* Params: message-literal, registrationIds-array, No. of retries, callback-function
**/
sender.send(message, registrationIds, 4, function (err, result) {
console.log(result);
});
view raw node-gcm.js hosted with ❤ by GitHub



위와 같이 매우 간단한 nodeJS코드를 통해서 push를 날릴 수 있다는 것을 알 수 있다.


나머지 안드로이드 코드는 GCM push android project로 구글링하면 나온다.

'프로그래밍 > nodeJS' 카테고리의 다른 글