Identifying Sender of Upstream GCM Message

495 Views Asked by At

I am using Google Cloud Messaging with XMPP in order to have both downstream and upstream messages.

Only client side I get a token by doing this on a worker thread:

InstanceID instanceID = InstanceID.getInstance(this);

try {
    String token = instanceID.getToken(getString(R.string.gcm_senderID), GoogleCloudMessaging.INSTANCE_ID_SCOPE, null);

    send_token(token, getString(R.string.gcm_senderID));
} catch (IOException e) {
    e.printStackTrace();
}

I then send this token over to the server where it is received. I am able to send messages to the client with this token.

Then I can send an upstream message on the client side with this:

new AsyncTask<Void, Void, String>() {
    @Override
    protected String doInBackground(Void... params) {
        String msg;

        Bundle data = new Bundle();

        data.putString("message", message);

        try {
            messenger.send(getString(R.string.gcm_senderID) + "@gcm.googleapis.com", messageId.addAndGet(1) + "", data);
        } catch (IOException e) {
            e.printStackTrace();
        }

        msg = "Sent message";

        return msg;
    }
}.execute(null, null, null);

In the upstream message sent from the client, there is a from field, that seems to be a token as well. If I send a message to this from the server side, my phone receives it as well.

What confuses me is that the token in the from field is not equal to the one generated by the InstanceID service.

The first 18 characters or so are equal, but after that they are very different. As such, is there a good way to identify what device sent what message?

I could store the token generated by the Instance ID each time in the Bundle, but I was wondering if there might be any way to make the from field of the upstream message be consistent with the generated ID.

Edit: Using the deprecated register function, I was able to get a consistent registration ID.

String token = GoogleCloudMessaging.getInstance().register(getString(R.string.gcm_senderID));

But is there a way to do this with InstanceID?

1

There are 1 best solutions below

1
On

Calling GoogleCloudMessaging.getInstance(context).register(senderId) instead of getToken(senderId, "GCM") seems to resolve the issue, the XMPP server will then receive the correct token, every time, in the "from" property of the upstream message.

My device is running CyanogenMod, so the Google Play services app doesn't update automatically. Since the old register() work, this issue is likely caused by a bug in the google-play-services_lib when talking to an older version of the GMS app.

I've answered instead of comment with the vain hopes of an Google dev seeing this.