Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 :)

like image 518
DonP Avatar asked May 09 '13 08:05

DonP


People also ask

Do device tokens change?

Each device has one, unique token that is used for messaging. Tokens can expire or be updated.

Why do we need device token?

Yes, Here required device tokens if we want to send push notification whoever installed your app. My research we can save device tokens in back end at first time installation of your app that is better according to my understanding so that we can easy to send push notification across all devices.

What is Android device token?

A token uniquely identifies an app on a device. An app can call the getToken method to request a token from the Push Kit server.

How do I find my Android phones token?

Click on Settings and open Mobile Apps. Click on the Android App and make sure the Firebase API key has been configured. Click on Test Push and enter the device token for your test device. Add a test payload and send the test.


1 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.

like image 119
Eran Avatar answered Oct 19 '22 10:10

Eran