zapotec
zapotec

Reputation: 2638

GCM sends different RegistrationId everytime

I am having a little "problem" with the GCM service of Android. The point is that, every time I ask for a registration_id, the GCM server returns a different registration_id. The worst is that, after doing tests, both the old ones and the new ones are working properly!! (I send a PUSH message to the old ones and to the new ones and my app is receiving the push from every of them!!).

This is the AsyncTask I am using the register in the GCM Android server:

public class AltaGCM extends AsyncTask<Void, Void, Void> {

    @Override
    protected Void doInBackground(Void... params) {
        GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(context);
        int contador = 0;
        try{
           regId = gcm.register(CrisolGooglePlayServicesUtils.SENDER_ID);
        } catch (IOException e) {
            statusCode = -1;
        }
        return null;
    }

    @Override
    protected void onCancelled() {

    }

    @Override
    protected void onPreExecute() {
    }

    @Override
    protected void onPostExecute(Void result) {
        listener.respuestaGCM(statusCode, regId);
    }

    @Override
    protected void onProgressUpdate(Void... values) {

    }
}

I don't know what I am doing wrong, but, this is happening to me since 2 months (more or less). Before I never had this problem, GCM server used to return me the SAME registration_id (not every times, but sometimes).......

I don't know if to receive every time a different registration_id is normal or if it is something wrong I am doing.

Thanks a lot!

Upvotes: 0

Views: 218

Answers (1)

Distwo
Distwo

Reputation: 11749

Possible duplicate of GCM registering with two different working registration ids

Look at the answer provided by @nunofmendes

Sometimes Google changes the registration ID and you'll have multiple IDs associated. The server that sends the notification (your server) has to update the database with the new ID.

For more info check this document:

http://developer.android.com/google/gcm/adv.html

That says:

On the server side, as long as the application is behaving well, everything should work normally. However, if a bug in the application triggers multiple registrations for the same device, it can be hard to reconcile state and you might end up with duplicate messages.

GCM provides a facility called "canonical registration IDs" to easily recover from these situations. A canonical registration ID is defined to be the ID of the last registration requested by your application. This is the ID that the server should use when sending messages to the device.

If later on you try to send a message using a different registration ID, GCM will process the request as usual, but it will include the canonical registration ID in the registration_id field of the response. Make sure to replace the registration ID stored in your server with this canonical ID, as eventually the ID you're using will stop working.

Upvotes: 1

Related Questions