Well, I know that the registration id of server would expire. However, how about the device registration id?
If yes, how do the server and client know about that?
Now I just simply save the id into database and assume it would never change.
Btw, is the registration id of server associated with the device registration id? Why the tutorial from the internet store them in the pair
Short answer is Yes, as stated here.
The application should store this ID for later use. Note that Google may periodically refresh the registration ID, so you should design your application with the understanding that the REGISTRATION Intent may be called multiple times. Your application needs to be able to respond accordingly.
Basically what would have to happen is that your application needs some sort of communication with your server to notify it that it received a new id. What you could do is when the app receives a registration id, store it on some remote database along with some additional unique information about that device (or user credentials if app has authentication). When Google decides to change the registration id, just update the previous entry on the remote db with the new id. Then when your server needs to push a message to some device, it matches the device to an up-to-date registration id in the database and uses that id to push the notification.
Related
I had set up the GCM notificaion in android .
with in one year i am getting so many device id for same device.
Is there any way to validate the Regsitraion id
I know on sending the push notification to old id it will gives the new id .
But i need to clear my Db is there any way to do it?
First of all, if you handle registration to GCM in the client side properly, you should be getting many registration IDs for the same device.
You should store the registration ID in the app's shared preferences, and register again to GCM only when a new version of your app is installed on the device (that's what Google recommend to do).
To handle the case of your app being uninstalled and installed again, you can generate a unique instance identifier for each device. Send that identifier to your server along with the registration ID. Store that identifier in the device's external storage, so it won't get deleted when your app is uninstalled. Try to restore it from external storage (if it's available) when your app is installed. Now, if you get a new registration ID, but still have the original instance identifier, when you send them to your server, your server will see it already has an old registration ID for that instance identifier, and will replace the old registration ID with the new one (instead of creating a new record).
Second of all, if you fail to avoid the creation of duplicate registration IDs for the same device in your DB, your only option to detect them is by sending a GCM message to the registration ID and handling the returned canonical registration ID. You could, however, send a message with dry_run=true, which simulates the sending of a message without actually sending it. I just tested it with an old registration ID. The message wasn't delivered, but I got the response with the canonical registration ID.
dry_run If included, allows developers to test their request without actually sending a message. Optional. The default value is false.
I am doing an android application which uses gcm.I came to know that when the application is uninstalled and re installed there is no guarantee to receive the same registration id again.So what I did is while logging in I take one unique Id from application server and whenever new registration id is made I am able to replace that in application server.So what my question is, I am not using the canonical id for replacing the latest registration id in gcm server.Will it cause any problem? Please help me.
Your solution is basically good, though I'm not sure whether that unique Id your server generates is associated with the device or the logged in user. If it's the former, make sure you persist it on the device's external storage, so that it doesn't get lost when the app is uninstalled. If it's the latter, I'm not sure how you are handling the case of the same user logging in on multiple devices.
While your strategy is good for maintaining the current registration ID on both the device and your server, you should still handle canonical registration ID responses from Google just to be safe.
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.
Source : http://developer.android.com/google/gcm/adv.html#canonical
I am working on an android project which uses gcm. I came to know that if we uninstall the appfrom device and reinstalled most of the time the device gets new registration id.Then if we do not delete the old one from application server and update, The messages will be sending to both ids and in the response it will be showing canonical id is present.My question is, at this point message will be successfully send to that device or not?
When you receive a canonical registration ID in the response from Google, the message was accepted by the GCM server and the GCM server would attempt to deliver it to the device. Whether it is actually sent to the device depends on whether the device is available (i.e. connected to the internet).
So if your server sends a GCM message to both the old ID and the new ID, the device will probably get two messages.
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.
(Source)
You can overcome this problem by assigning a unique identifier to each instance of your application. If you store that identifier in the device's external storage, it won't be deleted when the app is uninstalled. Then you can recover it when the app is installed again. If you send this identifier to your server along with the registration ID, you can check if your server has an old registration ID for this identifier, and delete it.
#Eran We have two option either to remove older registration ids or update with Key and position of canonical id. I prefer to update... You can view working code of mine i have answered here Steps to Update Canonical Ids
I have an app published at the play store.
When I want to send a notification, some users (for example, me) receive duplicate messages.
I know that I have to handle it when I send the messages to recover the canonical ids and update the old registered id. But, Can I do something to fix this situation without sending any notification?
You should figure out a way to notify your server when GCM sends your app a new registration id.
Currently when you get a registration id from GCM, you send it to your server, which probably adds it to your DB without checking if that id should replace an old id.
In order to detect that case, and update the old id instead pf inserting a new one, you should assign your own unique id to each app instance, and send it to the server along with the reg id. You should persist that id in external storage, so that it survive even if your app is uninstalled and then installed again.
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