8

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?

Howli
  • 12,291
  • 19
  • 47
  • 72
user2597622
  • 135
  • 1
  • 2
  • 8
  • possible duplicate of [Handling registration ID changes in Google Cloud Messaging on Android](http://stackoverflow.com/questions/16838654/handling-registration-id-changes-in-google-cloud-messaging-on-android) – trante May 22 '14 at 10:20
  • @trante that SO question is ages old and a waste of time. – Micro May 21 '16 at 15:49

2 Answers2

15

I read the 2 reasons over here when you GCM Registration Id might change:

  1. You’ll need to re-register every device each time you update your app.
  2. 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.

Community
  • 1
  • 1
Shobhit Puri
  • 25,769
  • 11
  • 95
  • 124
12

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.

  • My app has "AppUpdateReceiver" to listen for an app update, to launch "AppUpdateService" which syncs the FCM registration to work around the issue. Is this bug still relevant with the latest FCM library, can I remove this code, because starting services like this is tricky with Oreo. – Singed Jan 26 '18 at 16:05