TMH
TMH

Reputation: 6246

Does an Android GCM device id ever change?

I'm building GCM into my app and I want to make sure I don't end up with duplicate device ID's.

Is there a 'best practice' for dealing with device ID's? My original though was when the app loads it calls gcm.register, and posts the ID to my server, and if the key isnt in my database, I'll store it, then when I send out a notification, loop through the DB and send the messages.

But I as wondering if the ID ever changes, I don't want to send multiple messages to one device.

Upvotes: 11

Views: 7720

Answers (3)

shantanu chandra
shantanu chandra

Reputation: 537

I think it would be better if you can have login based app and id validated or update at each login. I have a similiar app and its is working for me.

Upvotes: 0

Shobhit Puri
Shobhit Puri

Reputation: 26007

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.

In addition to @tyczj's answer of change of ID's on updating the app, Google says that it may also automatically refresh the ID's. If you read 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.

Just an addition info that for handling this case 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.

Upvotes: 6

tyczj
tyczj

Reputation: 73753

yes it is possible for the ID to change. If you take a look at the tutorial in the SDK, every time the app is updated to a new version it will go and get a new ID because the previous ID is not guaranteed to work.

specifically take a look at this page

http://developer.android.com/google/gcm/client.html

 Check if app was updated; if so, it must clear the registration ID
 since the existing regID is not guaranteed to work with the new
 app version.

Upvotes: 12

Related Questions