ToothlessGear / node-gcm

A NodeJS wrapper library port to send data to Android devices via Google Cloud Messaging
https://github.com/ToothlessGear/node-gcm
Other
1.3k stars 206 forks source link

Messages not deliveried after upgrade from 0.9.7 #150

Closed mauron85 closed 8 years ago

mauron85 commented 8 years ago

After upgrade from 0.9.7 to 0.11.1 I don't receive push notifications in my cordova app. According to node-gcm server messages are sent fine (success=1 and message_id is assigned), but they're not delivered to android device.

All node-gcm versions up to 0.11.1 are working with the old code for version 0.9.7, but not with new code.

For version 0.9.7 I have old code (which is still working)

        var msg = new gcm.Message();
        // Value the payload data to send...
        msg.addData('title', push.title);
        msg.addData('message', push.message);
        // msg.addData('type', push.type);
        // msg.addData('payload', JSON.stringify(push.payload));
        msg.addData('msgcnt','3'); // Shows up in the notification in the status bar
        // msg.addData('soundname','beep.wav'); //Sound to play upon notification receipt - put in the www folder in app
        msg.collapseKey = 'mapilary';
        // msg.delayWhileIdle = true; //Default is false
        msg.timeToLive = 3000;// Duration in seconds to hold in GCM and retry before timing out. Default 4 weeks (2,419,200 seconds) if not specified.

        return sender.sendAsync(msg, push.recipients, 4); // I've used bluebird promisify

And for 0.11.1 I've adjusted the code to:

        var sender = new gcm.Sender(settings.gcm.apikey);
        var msg = new gcm.Message({
            collapseKey: 'mapilary',
            contentAvailable: true,
            delayWhileIdle: true,
            timeToLive: 3, // Duration in seconds to hold in GCM and retry before timing out. Default 4 weeks (2,419,200 seconds) if not specified.
            notification: {
                title: push.title,
                body: push.body,
                icon: 'ic_launcher'
            }
        });

        return sender.sendAsync(msg, push.recipients);
hypesystem commented 8 years ago

What is this method, sendAsync, you are using? I assume that's the pomisify-version...

hypesystem commented 8 years ago

In your first example your message is in push.message, in the new code you are using push.body. Are you sure push.body is defined?

hypesystem commented 8 years ago

In the new code, timeToLive should still be in ms, you have defined it in seconds. Your messages won't be delivered in 3 ms, so it fails.

mauron85 commented 8 years ago

Thank you for your assistence. Here are the anwers.

What is this method, sendAsync, you are using? I assume that's the pomisify-version...

yes, sendAsync is promisified version of send (i'm using bluebird lib for this).

In your first example your message is in push.message, in the new code you are using push.body. Are you sure push.body is defined?

yes, it's defined. I've replaced variable name from message to body to better match with node-gcm and google gcm varlable names.

In the new code, timeToLive should still be in ms, you have defined it in seconds. Your messages won't be delivered in 3 ms, so it fails.

ok, need to try this one. will let you know if it helps.

mauron85 commented 8 years ago

timeToLive increased to 3000ms and messages still not delivered

mauron85 commented 8 years ago

I've joined together old with new code. Every time I send msg it works and everytime I send msg2 it is not.

function sendPush (push) {

    var sender = new gcm.Sender(settings.gcm.apikey);
    var msg = new gcm.Message();
    msg.addData('title', push.title);
    msg.addData('message', push.body);
    msg.addData('msgcnt','3'); // Shows up in the notification in the status bar
    msg.collapseKey = 'mapilary';
    msg.timeToLive = 3000;// Duration in seconds to hold in GCM and retry before timing out. Default 4 weeks (2,419,200 seconds) if not specified.

    var msg2 = new gcm.Message({
        collapseKey: 'mapilary',
        timeToLive: 3000, // Duration in seconds to hold in GCM and retry before timing out. Default 4 weeks (2,419,200 seconds) if not specified.
        notification: {
            title: push.title || 'title',
            body: push.body || 'body',
            icon: 'ic_launcher'
        }
    });

    console.log('Sending push notification', msg.toJson());
    console.log('Sending push notification2', msg2.toJson());

    return sender.sendAsync(msg, push.recipients); // this is working
    //return sender.sendAsync(msg2, push.recipients); this is not :-(
}

Also this is the result of message toJson() for old and new code.

Push message msg (old)

{ data:
   { title: 'Aktualizovaná zásielka',
     message: 'AD-55dc99e4022362072317520e',
     msgcnt: '3' }
}

Push message msg2 (new)

{ collapse_key: 'mapilary',
  time_to_live: 3000,
  notification:
   { title: 'Aktualizovaná zásielka',
     body: 'AD-55dc99e4022362072317520e',
     icon: 'ic_launcher' }
}
mauron85 commented 8 years ago

No sure why, but data works fine.

var msg2 = new gcm.Message({
    collapseKey: 'mapilary',
    timeToLive: 3000, // Duration in seconds to hold in GCM and retry before timing out. Default 4 weeks (2,419,200 seconds) if not specified.
    data: {
        title: push.title || 'title',
        message: push.body || 'body'
    }
});

BTW I'm using https://github.com/phonegap/phonegap-plugin-push on the client side (but should not be related to my problem)

mauron85 commented 8 years ago

And one more remark. Example in README.md is using timeToLive: 3. Which is confusing or incorrect.

hypesystem commented 8 years ago

I would appreciate a PR on the timeToLive in the README --- good catch!

Are you sure phonegap push supports the new notification-field? This was added in the most recent version of GCM. See here: https://developers.google.com/cloud-messaging/concept-options#notifications_and_data_messages

mauron85 commented 8 years ago

Maybe that's the problem - no support in phonegap. I have just posted my question on phonegap-plugin-push - https://github.com/phonegap/phonegap-plugin-push/issues/87

hypesystem commented 8 years ago

Cool :) Hope they can clear that up. Can I get you to make a PR on the README? :)

mauron85 commented 8 years ago

Sure.

https://github.com/ToothlessGear/node-gcm/pull/152

mauron85 commented 8 years ago

Now I'm confused, because google gcm docs states that time_to_live is in seconds.

hypesystem commented 8 years ago

Hum -- that is probably right. To be honest, I didn't check it, I just assumed your old example was correct, and I know that the timeToLive implementation in node-gcm hasn't changed.

mauron85 commented 8 years ago

No problem. Will cancel PR.

macdonst commented 8 years ago

This is not a bug in node-gcm. The push plugin for Cordova is expecting the notification to come via the data message format. I know this for certain as I'm the maintainer of the phonegap-plugin-push repo.

This issue should be closed.

macdonst commented 8 years ago

Fixed in the push plugin https://github.com/phonegap/phonegap-plugin-push/issues/87

hypesystem commented 8 years ago

Great --- thanks for the info, @macdonst.

In summary: notification works in phonegap-plugin-push 1.2.1.

mauron85 commented 8 years ago

Thank you guys for clarification. Great support from both of you.