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 208 forks source link

receiving "gcm.notification.title" in extras #279

Closed miqmago closed 7 years ago

miqmago commented 7 years ago

This is quite strange: in android I'm receivig extras as:

extras.getString("gcm.notification.title");

I've placed a console.log('node-gcm', message); just before https://github.com/ToothlessGear/node-gcm/blob/master/lib/sender.js#L16 and the console is like this:

node-gcm Message {
  params: 
   { collapseKey: undefined,
     priority: undefined,
     contentAvailable: false,
     delayWhileIdle: false,
     timeToLive: 2419200,
     restrictedPackageName: undefined,
     dryRun: false,
     data: undefined,
     notification: 
      { title: 'Hola',
        body: 'Missatge enviat desde postman',
        icon: undefined,
        sound: undefined,
        badge: undefined,
        tag: undefined,
        color: undefined,
        click_action: undefined,
        body_loc_key: undefined,
        body_loc_args: undefined,
        title_loc_key: undefined,
        title_loc_args: undefined } } }

So it seems good to me. I'm mainaining the cordova-push-notifications plugin and it relies on extras.getString("message") to handle notifications while app is in background (see https://github.com/appfeel/cordova-push-notifications/blob/master/src/android/com/plugin/gcm/GCMIntentService.java#L75).

If this is the new specification I could modify the plugin, but I'm not sure if this is new standard, or if something should be changed in calling code (I'm also maintaining https://github.com/appfeel/node-pushnotifications and it depends on this plugin for android, so it should be changed there as well), or if this is an issue of this plugin or if that depends on google.

eladnava commented 7 years ago

There are 2 types of payloads you can receive with GCM on Android:

1) "data" payload (not visible to the user by default) 2) "notification" payload (visible as a system notification, may contain title, message, etc)

The JSON object you supply via "data" is accessible to the Intent extras of your push BroadcastReceiver directly (i.e. intent.getStringExtra("id") for sending data = {id: 1}).

The notification object, on the other hand, is accessible via intent.getStringExtra("gcm.notification.x") as you discovered.

It all depends on which one you actually want to send and interact with.

miqmago commented 7 years ago

Thanks! Is this something related to google or to this plugin? I'm interested on this in order to know if I have to give backwards compatibility to the cordova plugin or I can definetely modify it.

eladnava commented 7 years ago

@miqmago It is the way GCM works. The way GCM passes both of these payloads to your BroadcastReceiver is by prefixing the notification keys with gcm.notification.

miqmago commented 7 years ago

Thanks!