dimanche 26 juillet 2015

GCM with Ember - In Chrome App

Am building a chrome app with Ember-CLI (version 1.11). Am using Google Cloud Messaging (GCM) in the chrome app. I have set up the GCM to work and receiving push messages.

But am able to receive the push messages only in the background script (background.js) of Chrome app.

The chrome app manifest file

{
    "manifest_version": 2,
    "name": "Still Unnamed",
    "version": "1",
    "icons": {
        "128": "chrome-icon.png"
    },
    "permissions": ["fullscreen","http://localhost:8000/","gcm","storage"],
    "app": {
        "background": {
            "scripts": ["background.js"],
            "persistent": false
        }
    },
    "minimum_chrome_version": "28"

}

background.js

chrome.app.runtime.onLaunched.addListener(function() {
    chrome.app.window.create('index.html', {
        id: 'main',
        bounds: { width: 1920, height: 1080 },
        state: "fullscreen"
    });
    console.log("starting");
    chrome.storage.local.get("registered", function(result) {
        // If already registered, bail out.
        if (result["registered"])
            return;

        // Up to 100 senders are allowed.
        var senderIds = ["7805834xxxxx"];
        chrome.gcm.register(senderIds, registerCallback);
    });
});

chrome.app.runtime.onRestarted.addListener(function() {
    console.log("Restarted");
});

chrome.gcm.onMessage.addListener(function(message) {
    console.log(message);
});

function registerCallback(registrationId) {
    if (chrome.runtime.lastError) {
        // When the registration fails, handle the error and retry the
        // registration later.
        return;
    }
    console.log("Registered " + registrationId);

    // Send the registration token to your application server.
    sendRegistrationId(function(succeed) {
        // Once the registration token is received by your server,
        // set the flag such that register will not be invoked
        // next time when the app starts up.
        if (succeed)
            chrome.storage.local.set({registered: true});
    });
}

function sendRegistrationId(callback) {
    // Send the registration token to your application server
    // in a secure way.
}

Everything is well and good except am not able to listen to these push messages inside my ember app.

Things I have tried so far

  • Add the gcm.onMessage listener to didInsertElement in Ember.View of the application route. This didn't work.
  • I have a solution in my mind. To store the message and a isThereANewMessage flag to the chrome/local storage and do a heartbeat check for new messages in chrome storage/local storage from my ember app. But am doing this only when everything else fails.

So, how do I listen to these push messages in my ember app using gcm.onMessage




Aucun commentaire:

Enregistrer un commentaire