Created
July 9, 2012 22:26
-
-
Save j4rs/3079447 to your computer and use it in GitHub Desktop.
NodeJS GCM Server to send message to android devices...
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('./gcm'); | |
var gcm = new GCM(<YOUR GOOGLE API KEY>); // https://code.google.com/apis/console | |
// create the message | |
var msg = { | |
registration_ids: [token], // this is the device token (phone) | |
collapse_key: "your_collapse_key", // http://developer.android.com/guide/google/gcm/gcm.html#send-msg | |
time_to_live: 180, // just 30 minutes | |
data: { | |
message: "Hello mundo cruel :P" // your payload data | |
} | |
}; | |
// send the message and see what happened | |
gcm.send(msg, function(err, response) { | |
// that error is from the http request, not gcm callback | |
console.log(response); // http://developer.android.com/guide/google/gcm/gcm.html#response | |
}); |
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 request = require('request'); | |
var GCM = function(api_key) { | |
this._api_key = api_key; | |
} | |
GCM.prototype.send = function(msg, callback) { | |
request.post({ | |
uri: 'https://android.googleapis.com/gcm/send', | |
json: msg, | |
headers: { | |
Authorization: 'key=' + this._api_key | |
} | |
}, function(err, response, body) { | |
callback(err, body); | |
}) | |
} | |
module.exports = GCM; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment