2

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?

  1. Will I get the same Registration ID for the same App on the same device once the user re-installs the app?

  2. Will Google invalidate these Registration IDs after some time?

Eran
  • 387,369
  • 54
  • 702
  • 768
user1537779
  • 2,311
  • 5
  • 28
  • 45
  • see this http://www.androidhive.info/2012/10/android-push-notifications-using-google-cloud-messaging-gcm-php-and-mysql/ you will get answer – Ronak Mehta May 31 '13 at 04:59

2 Answers2

1

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.

Pankaj Kumar
  • 81,967
  • 29
  • 167
  • 186
  • Any link on `GCM gives you the idea about handleing of updated registartion ID`? – user1537779 May 31 '13 at 05:23
  • these are two links http://stackoverflow.com/questions/13689530/how-to-handle-gcm-changing-the-registration-id http://stackoverflow.com/questions/13752995/gcm-and-id-handling can help you. – Pankaj Kumar May 31 '13 at 05:29
  • Okay, using the canonical_id. So, when I get a canonical_id I will have to update my database and nothing needed on android side right? – user1537779 May 31 '13 at 05:33
  • No not need if you are not storing reg_id on device too... :) – Pankaj Kumar May 31 '13 at 05:34
  • One more thing you can do here... when reg_id gets updated you will get `com.google.android.c2dm.intent.REGISTRATION` again. SO if you are saving reg_id on device also, you can check here if reg_id exists already it means new id is updated reg_id otherwise that is new reg_id.. got it? – Pankaj Kumar May 31 '13 at 05:37
  • fine. But I am using GCM helper class. Anyways I am storing the reg_id just to make sure the device is already registered. – user1537779 May 31 '13 at 06:03
  • Thats ok... then you can easily implement reg_id update handling functionality too. If needed. – Pankaj Kumar May 31 '13 at 06:05
1

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
Eran
  • 387,369
  • 54
  • 702
  • 768