Last active
May 20, 2017 22:18
-
-
Save AkarshSatija/392fca1bd873ff86dbe2 to your computer and use it in GitHub Desktop.
Node GCM
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 http = require('http'); | |
var data = { | |
"collapseKey":"applice", | |
"delayWhileIdle":true, | |
"timeToLive":3, | |
"data":{ | |
"message":"My message","title":"My Title" | |
}, | |
"registration_ids":["token goes here"] | |
}; | |
var dataString = JSON.stringify(data); | |
var headers = { | |
'Authorization' : 'key=Auth Key goes here', | |
'Content-Type' : 'application/json', | |
'Content-Length' : dataString.length | |
}; | |
var options = { | |
host: 'android.googleapis.com', | |
port: 80, | |
path: '/gcm/send', | |
method: 'POST', | |
headers: headers | |
}; | |
//Setup the request | |
var req = http.request(options, function(res) { | |
res.setEncoding('utf-8'); | |
var responseString = ''; | |
res.on('data', function(data) { | |
responseString += data; | |
}); | |
res.on('end', function() { | |
var resultObject = JSON.parse(responseString); | |
print(responseString); | |
console.log(resultObject); | |
}); | |
console.log('STATUS: ' + res.statusCode); | |
console.log('HEADERS: ' + JSON.stringify(res.headers)); | |
}); | |
req.on('error', function(e) { | |
// TODO: handle error. | |
console.log('error : ' + e.message + e.code); | |
}); | |
req.write(dataString); | |
req.end(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Key posted openly! :P