Skip to content

Instantly share code, notes, and snippets.

@davydes
Last active January 26, 2017 09:50
Show Gist options
  • Save davydes/60bcd08343d3dc00e408f9b7c842540a to your computer and use it in GitHub Desktop.
Save davydes/60bcd08343d3dc00e408f9b7c842540a to your computer and use it in GitHub Desktop.
Example Cordova Push Plugin With Parse Server
// Plugin homepage https://github.com/phonegap/phonegap-plugin-push
var app = {
initialize() {
document.addEventListener('deviceready', this.onDeviceReady.bind(this), false);
},
addPushService() {
if(window.PushNotification){
PushNotification.hasPermission((data) => {
if (data.isEnabled) {
console.log('Push isEnabled');
this.push = PushNotification.init({
"android": {
"senderID": "1066475541683"
},
"ios": {
"alert": "true",
"badge": "true",
"sound": "true"
}
});
this.push.on('notification', (data) => {
console.log(data.message);
console.log(data.title);
console.log(data.count);
console.log(data.sound);
console.log(data.image);
console.log(data.additionalData);
});
this.push.on('registration', (data) => {
let url = 'http://parse-server.flynn.molinos.ru/parse/installations';
let token = data.registrationId;
let body = {
deviceType: "android",
deviceToken: token,
userId: "Some id of current user"
}
body = JSON.stringify(body);
console.log("Request body: " + body);
let xhr = new XMLHttpRequest();
xhr.open("POST", url, true);
xhr.setRequestHeader('Content-Type', 'application/json');
xhr.setRequestHeader('X-Parse-Application-Id', '123');
xhr.setRequestHeader('X-Parse-Master-Key', '123');
xhr.onreadystatechange = function() {
if (xhr.readyState == 4) {
if(xhr.status == 200 || xhr.status == 201) {
console.log("Registering NEW device successfull");
console.log("Response: " + xhr.responseText);
} else {
console.log("Ups, we are screwed!");
}
}
}
xhr.send(body);
});
}
});
}
},
onDeviceReady() {
console.log('device ready');
this.addPushService();
}
};
app.initialize();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment