GCM messages not getting delivered to Chrome Packaged Apps(offline mode)

本秂侑毒 提交于 2020-01-07 04:32:05

问题


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



来源:https://stackoverflow.com/questions/33409690/gcm-messages-not-getting-delivered-to-chrome-packaged-appsoffline-mode

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!