0

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")        
});
Sushant Kr
  • 1,953
  • 14
  • 19
  • I don't think the user can get the `GCM push notification` when the chrome is closed, just like Hangouts on chrome. But I think it will saved somewhere, and you can retrieve it during its ttl(`time to live`) – bjiang Oct 29 '15 at 17:35
  • Found the solution. The problem was the way I was assuming the chrome behaves(comparing it with mobile apps). – Sushant Kr Oct 29 '15 at 20:00

1 Answers1

0

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

Sushant Kr
  • 1,953
  • 14
  • 19