GCM messages not getting delivered to Chrome Packa

2019-09-05 00:02发布

问题:

I am using https://github.com/ToothlessGear/node-gcm to send message using GCM to chrome packaged apps.

Everything works fine if chrome app is running

When I am sending the message while chrome app is closed, those messages are not delivered after starting the app. I do get the message_id in the result at server.

I also tried using command line, facing the same problem in command line too.

Code at server side(nodejs)

var GCM = require('node-gcm');
var gcmSender = new GCM.Sender(apiKey);
var sendGCMMessage = function(data, regIds, collapseKey, callback) {

                var message = new GCM.Message({
                    priority: 'high',
                    collapseKey: collapseKey,
                    data: data                  
                });

                gcmSender.send(message, regIds, 
                    function(err, result) {
                    callback(err, result)
                })

    }

Packaged Apps:

//register 

var senderIds = [senderId];
chrome.gcm.register(senderIds, function(registrationId) {

    sendRegistrationIdToServer(registrationId, function(succeed) {

    });
});

    //listen to incoming messages

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

回答1:

The problem was the way I was assuming the chrome behaves.

In chrome the GCM message is delivered if chrome is running(and app is closed). chrome wakes up the app and run the onMessage listener, given that the registration took place in the background.js. Or else the message is discarded.

In my case I was not doing the registration in backgroud.js. Moved my registration to backgroud.js and it is working fine now