Created
April 18, 2016 17:32
-
-
Save peatiscoding/69e8ffdad35af7d5e809e43fed9f4894 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| function GCMCourier(apiKey) { | |
| this.apiKey = apiKey; | |
| this.push = function(notification, callback) { | |
| var xhttp = new XMLHttpRequest(); | |
| xhttp.onreadystatechange = function() { | |
| if (xhttp.readyState==4) { | |
| if (xhttp.status == 200) { | |
| var output = xhttp.responseText; | |
| callback && callback(true, output); | |
| } else { | |
| callback && callback(false, xhttp.responseText, xhttp.status); | |
| } | |
| } | |
| } | |
| xhttp.open('POST', 'https://gcm-http.googleapis.com/gcm/send', true); | |
| xhttp.setRequestHeader('Content-Type', 'application/json'); | |
| xhttp.setRequestHeader('Authorization', 'key='+apiKey); | |
| xhttp.send(JSON.stringify(notification)); | |
| }; | |
| }; |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage:
For more info please refer to: http://peatiscoding.me/geek-stuff/javascript-to-deliver-gcm-payload/