I developing the application that use the GCM. As mentioned in the documentation, after the application update need to invalidate the existing registration ID and start the registration process again.
Does this mean that I need to make unregistration before making the new registration?
No, you don't have to unregister before registering again. In fact, in most cases, when you register again you will get the same Registration ID you had in your previous registration. By "invalidate the existing registration ID" they mean that after an application update, the app should clear the Registration ID it has stored locally and request a Registration ID from GCM again (by calling GoogleCloudMessaging.register).
If, however, you unregister and then register again, you increase the chances that GCM would assign the device a new Registration ID, which means you have to remove the old Registration ID from your server's DB (to avoid sending duplicate messages to the same device).
Related
I wonder what is the lifecycle of registrationId(device_token)
e.g eD3Fa1yVqx8:APA91bH5gNrC-jhUqaoRwyFLfD2ik4NXwCXohwhm_9CM5hnY9wFwUAOiO_O12Or-dm60sUqy9gN2ZW6mw5i90RyNhb-zHilvtcJczPjZoQlm_4lKNKDejC_1_xiqmYoZnSxaFfVqSA1d
How to handle the situation if it's changed?
As far as the information on the official docs goes, there is no information about the token being changed by Google. However you should change it periodically.
Quoting https://developers.google.com/cloud-messaging/registration#automatic-retry-using-exponential-back-off
Keeping the Registration State in Sync
To protect the client app and app server from potential malicious
re-use of registration tokens, you should periodically initiate token
refresh from the server. When GCM registration token refresh is
initiated from server side, the client app must handle a
tokenRefreshed message with the GCM registration client/server
handshake.
I faced some situations where Registration ID changed. But, i don't know how and when.
So, to handle these situations i updated my app to send Registration ID every time the app is launched. I sent Registration ID with device id and check my database if the registration id changed i updated it.
In order to get an updated registration ID once a user upgrades their android version, do I need to first unregister the original registration ID by calling unRegister before I get the new registration ID?
You don't have to un-register before registering again to GCM, unless you are changing the sender ID (project number) used to register. In fact, the registration ID may even stay the same if you register with the same sender ID.
You don't have to do that and if you do the registration ID will remain the same.
If you want to have a completely new and different registration-id you need to register with another project-id.
Also, if you change your package name for your project, the registration-id would change too.
Updating registration ID is not required if Android device get updated with newer Android version e.g. JellyBean to KitKat.
You have to update registration ID if your application is updated. In that case you don't have to un-register at all, just register again and clear stored previous ID in the local storage.
Android Developer page has well written chapter about GCM Advanced Topics. You should be interested in Unregistration and Keeping the Registration State in Sync
#Eran is absolutely right.Upgrading your android version won't change gcm registration id.However sometimes may be because of some bug in your app or uninstalling(and again reinstalling the app) gcm may change your registration id.But thankfully gcm provides the concept of canonical ids.So i don't think you will need to unregister your device from gcm for this case but you need to deal with the canonical id properly if it occurs.
Please refer here and have a read about canonical ids
Canonical IDs
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.
I am just started to learn the GCM in android. I did not find this in the documentation that Does Google keep the GCM Registration ID always?.
Can a developer remove it, any way to access this database programmatically?
Besides the periodic refresh(which is rare),
An existing registration ID may cease to be valid in a number of
scenarios, including: If the application manually unregisters by
issuing a com.google.android.c2dm.intent.UNREGISTER intent. If the
application is automatically unregistered, which can happen (but is
not guaranteed) if the user uninstalls the application. If the
registration ID expires. Google might decide to refresh registration
IDs. For all these cases, you should remove this registration ID from
the 3rd-party server and stop using it to send messages. Happens when
error code is NotRegistered.
About the periodic refresh:
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
Read more:
http://developer.android.com/google/gcm/gcm.html
http://developer.android.com/google/gcm/adv.html#reg-state
Do GCM registration id's expire?
I developed an application which used GCM technology and everything is OK.
I observed that the registration ID of the device changes after a period of time and this has caused a problem in my app because my app is dependent on the Reg ID.
So how I can get a fixed Reg ID for clients?
I read the 2 reasons over here when you GCM Registration Id might change:
You’ll need to re-register every device each time you update your
app.
You’ll also need to re-register a device if the version of Android it’s running has been updated
P.S: The below old answer's reference has been removed from Google's page, so might not be valid anymore
If you see the second point under the heading Enable GCM on Architectural Overview page, it says:
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.
So, for handling that you should have a Broadcast Listener which could handle com.google.android.c2dm.intent.REGISTRATION intent, which Google send to the app when it has to refresh the registration ID. The broadcast receiver will have the onReceive method with an Intent. From the intent you can get the Bundle using which you can extract the new registration ID from Google. You can save that and send it to the 3rd part server to replace your previous registered ID for that user.
Also you may see this answer on the question In GoogleCloudMessaging API, how to handle the renewal or expiration of registration ID?.
Discussion on Should applications call gcm.register() every seven days to ensure valid registration IDs? question might also be of some use.
Hope this helps you understand how to handle it.
The 'periodical' refresh never happened, and the registration refresh is not included in the new GCM library.
The only known cause for registration ID change is the old bug of apps getting unregistered automatically if they receive a message while getting upgraded. Until this bug is fixed apps still need to call register() after upgrade, and so far the registration ID may change in this case. Calling unregister() explicitly usually changes the registration ID too.
The suggestion/workaround is to generate your own random identifier, saved as a shared preference for example. On each app upgrade you can upload the identifier and the potentially new registration ID. This may also help tracking and debugging the upgrade and registration changes on server side.
I just started to use Google Cloud Messaging and read about User Notifications.
According to this link, all the devices owned by a particular user will be notified at once.
In my case, a particular user is identified by his/her user_id and the database would look like this:
[user_id] [gcm_registration_id]
As per the demo, on the application side this registration_id is stored as persistent data.
What happens if the user uninstalls the app and the persistent data is gone?
Will I get the same Registration ID for the same App on the same device once the user re-installs the app?
Will Google invalidate these Registration IDs after some time?
will I get the same registration_id for the same app on the same
device once the user reinstalls the app?
YES. Reg id chnages in two cases. Either your app will register OR Google refreshes the Registration ID. SO until any one cases executes your are fine with old reg id.
By any chance google invalidates these registration_ids after some
time?
YES. Google refreshes the registration ID. GCM gives you the idea about handleing of updated registartion ID.
Handle updated id on client side
If the registration is successful, the GCM server broadcasts a com.google.android.c2dm.intent.REGISTRATION intent which gives the Android application a registration ID.
The Android application should store this ID for later use (for instance, to check on onCreate() if it is already registered). 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.
Actually the answer you accepted is not entirely correct.
I tested the case of un-installing and re-installing an app on a device, and in some cases you could get a different registration ID.
There are two cases :
You install an app, register to GCM and get a registration ID.
You un-install app
Your server sends a few messages to that registration ID, until you get a NotRegistered error from GCM (I believe you'll get that error only from the 2nd message you send).
You re-install the app
You receive a new registration ID when app register to GCM
If you now send a message with the old registration ID, it will still work, but you'll get the new registration ID in the response as canonical registration ID.
You install an app, register to GCM and get a registration ID.
You un-install app
The server doesn't send anything to that registration ID while the app is un-installed.
You re-install the app
You receive the same registration ID when app registers to GCM