I have implemented GCM in my project using guidelines from https://developers.google.com.
Steps that I have take are:
For receiving device Token:
-> Class
GCMRegistrationIntentService
which extendsIntentService
and it is started from my mainActivity.-> In this service I have used:
InstanceID instanceID = InstanceID.getInstance(getApplicationContext()); token = instanceID.getToken(AppConstants.SENDER_ID, GoogleCloudMessaging.INSTANCE_ID_SCOPE, null);
I have received the token successfully.
LocalBroadcastListener in my main activity to get the token generated in step 1 and store it in shared preference (Also send it to my message Server).
Class
GCMTokenRefreshListenerService
which extendsInstanceIDListenerService
to get the token in case old one expired.-> In this I made a call to
GCMRegistrationIntentService
inonTokenRefresh()
Now my questions are:
- In which case GCMTokenRefreshListenerService be called?
- What is instanceID? What is lifecycle of instanceID?
I ran the service from terminal using:
./adb shell am startservice -a com.google.android.gms.iid.InstanceID --es "CMD" "RST" -n package.name/service.name.
Which gave me new token every time. How do I save this newly generated token in my shared preference?
When I receive new token from this service, I don’t receive notification as my token is changed. I will have to open my app in order to update the token. How to update this token?
- Do I have to call GCM every time when my app opens to get the token?