ChrisMcJava
ChrisMcJava

Reputation: 2303

node-gcm sending successfully but I do not know how to get the data from the client side

So I am sending on the server side like so, using node-gcm
(https://github.com/ToothlessGear/node-gcm):

var gcm = require('node-gcm');

var message = new gcm.Message({
    collapseKey: 'demo',
    delayWhileIdle: true,
    timeToLive: 3,
    data: {
        type: 'pong',
        message: 'Hello Android!'
    }
});

var sender = new gcm.Sender(myAPIKey);
var registrationIds = [aDoc.registrationId];

sender.send(message, registrationIds, 4, function (err, result) {
    console.log(result);
});

And then on the client side, in my BroadcastReceiver, I get the Logcat message printing and the receive notification, but the extras (from the intent.getStringExtra("data") is null. How do I get it properly? I cannot find how to do it anywhere. The registration case works perfectly.

@Override
protected void onHandleIntent(Intent intent) {
    String action = intent.getAction();
    Log.i(TAG, "GCM Received");

    switch(action){
    case GCM.ACTION_REGISTRATION:
        String registrationId = intent.getStringExtra(GCM.KEY_REG_ID);
        Log.i(TAG, "action received: registration: " + registrationId);
        ...
        break;
    case GCM.ACTION_RECEIVE:
        String extras = intent.getStringExtra("data");
        Log.v(TAG, "" + extras);
        ...
...

extras is consistantly null

Upvotes: 1

Views: 2658

Answers (1)

ChrisMcJava
ChrisMcJava

Reputation: 2303

Through experimentation, I have solved my problem. Instead of accesssing the data object directly, node-gcm adds the key-value pairs to the intent's extras directly:

Wrong:

// returns null
String dataJson = intent.getStringExtra("data");

Right:

// returns "pong"
String type = intent.getStringExtra("type");

Upvotes: 1

Related Questions