Skip to content

Instantly share code, notes, and snippets.

@mimoccc
Forked from j4rs/client.js
Created September 10, 2013 12:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mimoccc/6508928 to your computer and use it in GitHub Desktop.
Save mimoccc/6508928 to your computer and use it in GitHub Desktop.
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
});
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