Should I require a new token every time I launch an Android app?

I know that in Android, both in iOS and WP7 ( In which mobile platforms is the “token token” not constant?), The token provided to the CAN device is changed.

This means that we must deal with this by requiring a new token, at least every time our application starts.

However, this leads to a complete contradiction with what I have found so far in googles tutorials. (I can't remember exactly where I found this code, but I'm sure it was provided by Google). The code looks like this: this:

//registering for push GCMRegistrar.checkDevice(this); GCMRegistrar.checkManifest(this); final String regId = GCMRegistrar.getRegistrationId(this); if (regId.equals("")) { GCMRegistrar.register(this, SENDER_ID); Log.i("****************","I just Registered!!"); } else { Log.i("****************","Already registered"); } 

The above code will execute the line GCMRegistrar.register(this, SENDER_ID); only once, at the first launch of the application. But I guess this is wrong? Because if the token changes, there is no way for our application to see this, because it requires the token only once.

EDIT


Actually, I just found where exactly this code was. Take a look here: Getting Started Guide

I quote:

In the onCreate () method, add the following code:

 GCMRegistrar.checkDevice(this); GCMRegistrar.checkManifest(this); final String regId = GCMRegistrar.getRegistrationId(this); if (regId.equals("")) { GCMRegistrar.register(this, SENDER_ID); } else { Log.v(TAG, "Already registered"); } 

When registering a device, the onRegistered(Context context, String regId): function is onRegistered(Context context, String regId): . This function is described in the tutorial:

onRegistered (Context context, String regId): Called after receiving a registration application, passes the registration identifier assigned by GCM to this pair of devices / applications as a parameter. Typically, you should send regid to your server so that it can use it to send messages to this device.

So, he says that I should send this identifier to my server. But earlier the code showed us that this function will be called only once! So what happens when the token changes? How am I going to update it on my server? I think I should run this function every time the application starts ...

Am I missing something here? Is the code incorrect? Any thought would be helpful :)

+7
source share
1 answer

Google will inform you that if the registration ID changes when you are already registered, the registration will be sent again, so your application will receive a new registration ID in onRegistered without having to request a new registration every time it starts.

Please note that Google may periodically update the registration identifier, so you should design your Android application with the understanding that the intention com.google.android.c2dm.intent.REGISTRATION can be called several times. Your Android app should be able to respond accordingly.

(taken from here )

Therefore, the code above must be accurate. In fact, this code will return an empty registration identifier if you upgrade your application, which will result in a new registration.

However, it seems to me that it’s safer for me to register every time the application starts.

+9
source

All Articles