Reputation: 6246
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
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
Reputation: 26007
I read the 2 reasons over here when you GCM Registration Id might change:
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
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