Home » Android » push notification – Should i require a new token every time i launch my Android App?

push notification – Should i require a new token every time i launch my Android App?

Posted by: admin June 15, 2020 Leave a comment

Questions:

I know that in Android as in iOS and WP7 (In which mobile platforms the “push token” is not permanent?) the push token provided to the device CAN change.

That means we should handle this by requiring a new token ,at least every time our application launches.

However , this comes to complete contradiction to what i ‘ve found so far in googles tutorials.(I cant remember where exactly i ‘ve found this code , but i am pretty sure it was provided by google) The code looks like 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 code above will execute the GCMRegistrar.register(this, SENDER_ID); line only once , the first time that the application launches. But i guess this ir wrong right? Because if the token changes , there is not way that our application will see that as it requires a token only once..

EDIT


Actually i just found where exactly was that code. Take a look here : Get Started Guide

I am quoting :

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 you register the device , the onRegistered(Context context, String regId): function is called. About this function the tutorial says :

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

So it says that i should send this id to my server. But the code before showed us that this function will be called just once ! So what happens when the token changes? How am i going to refresh it , to my server ? I think i should run this function every time the app launches …

Am i missing something here? Is the code wrong? Any thought would be helpful 🙂

How to&Answers:

Google say that if the registration id changes while you’re already registered, the registration ïntent will be sent again, so your app will get the new registration id in ‘onRegistered’ without having to request a new registration on each launch.

Note that Google may periodically refresh the registration ID, so you should design your Android application with the understanding that the com.google.android.c2dm.intent.REGISTRATION intent may be called multiple times. Your Android application needs to be able to respond accordingly.

(taken from here)

Therefore the above code should be fine. Actually this code will return a null registration id if you update your app version, which will trigger a new registration.

That said, it seems safer to me to register each time the app is launched.