Skip to content

Instantly share code, notes, and snippets.

@davydes
Last active January 26, 2017 09:50
Show Gist options
  • Select an option

  • Save davydes/60bcd08343d3dc00e408f9b7c842540a to your computer and use it in GitHub Desktop.

Select an option

Save davydes/60bcd08343d3dc00e408f9b7c842540a to your computer and use it in GitHub Desktop.

Revisions

  1. davydes revised this gist Jan 26, 2017. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions cordova_app.js
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,5 @@
    // Plugin homepage https://github.com/phonegap/phonegap-plugin-push

    var app = {
    initialize() {
    document.addEventListener('deviceready', this.onDeviceReady.bind(this), false);
  2. davydes revised this gist Jan 26, 2017. No changes.
  3. davydes created this gist Jan 26, 2017.
    74 changes: 74 additions & 0 deletions cordova_app.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,74 @@
    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();