node-gcm npm은 nodejs에서 GCM notification push를 효과적으로 이루어지게 도와주는 모듈이다.
npm node-gcm 공식 사이트 : https://www.npmjs.com/package/node-gcm
node-gcm npm을 깔고 id와 apiKey를 등록하고, 안드로이드가 준비되어 있다면 아래와 같은 nodeJS코드를 활용하면 된다.
This file contains 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 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); | |
}); |
위와 같이 매우 간단한 nodeJS코드를 통해서 push를 날릴 수 있다는 것을 알 수 있다.
나머지 안드로이드 코드는 GCM push android project로 구글링하면 나온다.
'프로그래밍 > nodeJS' 카테고리의 다른 글
nodeJS에서 특정 시간에 작업 수행하기 (0) | 2016.06.25 |
---|