Here is the scenario of the problem I am facing right now. I have a web app that holds all the registration id
of devices that connected to my server. On my android app. Every time the user logs in, I send request for registration id
to google, forward to my server, then the server saves it and link it to the user. When a user logs out from my app. My app sends a request to the server to destroy the authentication code
and registration id
. The problem was, when the user uninstalls the app and a reinstall it again, his old registration id
retains on my server. So when he logged in on his same phone. the app registers the new registration id
to my server, when the server pushes a notification, that user would receive multiple notification depending on how many times he uninstalled the app. What is your work around on this one?

- 39
- 4
-
You can apply check on the basis of device Id everytime when user is logging in and sending request to server send device id as well so if device and user id are same then update that entry instead of new insert. – Preetika Kaur Sep 20 '16 at 08:13
4 Answers
Refer Official document.
You have to check this on your server. You cannot do it from the application code since there is no way of knowing when the user is uninstalling the application.
when you send a push notification, GCM will check if the user has your application, if the user has uninstalled the application GCM will note the same and inform you as part of reply for the push.
EDIT - From GCM docs
How uninstalled client app unregistration works
A client app can be automatically unregistered after it is uninstalled. However, this process does not happen immediately. What happens in this scenario is:
- The end user uninstalls the client app.
- The app server sends a message to GCM connection server.
- The GCM connection server sends the message to the GCM client on the device.
- The GCM client on the device receives the message and detects that the client app has been uninstalled; the detection details depend on the platform on which the client app is running.
- The GCM client on the device informs the GCM connection server that the client app was uninstalled.
- The GCM connection server marks the registration token for deletion.
- The app server sends a message to GCM.
- The GCM returns a NotRegistered error message to the app server.
- The app server should delete the registration token.
Note that it might take a while for the registration token to be completely removed from GCM. Thus it is possible that messages sent during step 7 above get a valid message ID as a response, even though the message will not be delivered to the client app. Eventually, the registration token will be removed and the server will get a NotRegistered error, without any further action being required from the app server.

- 3,711
- 8
- 32
- 43
-
-
The only info in response I could get was the canonical id which will be the new registration id. – Haunter Sep 20 '16 at 09:29
From the Official Documentation:
A client app can be automatically unregistered after it is uninstalled. However, this process does not happen immediately. What happens in this scenario is:
- The end user uninstalls the client app.
- The app server sends a message to GCM connection server.
- The GCM connection server sends the message to the GCM client on the device.
- The GCM client on the device receives the message and detects that the client app has been uninstalled; the detection details depend on the platform on which the client app is running.
- The GCM client on the device informs the GCM connection server that the client app was uninstalled.
- The GCM connection server marks the registration token for deletion.
- The app server sends a message to GCM. The GCM returns a NotRegistered error message to the app server.
- The app server should delete the registration token.
Note that it might take a while for the registration token to be completely removed from GCM. Thus it is possible that messages sent during step 7 above get a valid message ID as a response, even though the message will not be delivered to the client app. Eventually, the registration token will be removed and the server will get a NotRegistered error, without any further action being required from the app server.
Check this our: Downstream messages response codes
Unregistered Device
200 + error:NotRegistered
An existing registration token may cease to be valid in a number of scenarios, including:
- If the client app unregisters with FCM.
- If the client app is automatically unregistered, which can happen if the user uninstalls the application. For example, on iOS, if the APNS Feedback Service reported the APNS token as invalid.
- If the registration token expires (for example, Google might decide to refresh registration tokens, or the APNS token has expired for iOS devices).
- If the client app is updated but the new version is not configured to receive messages.
For all these cases, remove this registration token from the app server and stop using it to send messages.

- 24,761
- 25
- 106
- 174
This can be done on your server side code.
While inserting requested data to your GCM table, check if your user id is already in table? if yes simply replace whole row with new data. else insert fresh new data into table.
Side-note : it is better practice to check for duplicate GCM registration id as well. because google refresh all GCM ids frequently.
Hope this will help.
EDIT
as @Haunter suggested,
what if the users have a multiple devices?
Well in this case use store IMEI number on your database and check for that instead of user id.
how to get IMEI number
TelephonyManager tel = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
String IMEI = tel.getDeviceId().toString();

- 5,518
- 9
- 31
- 50
-
There is a problem with this implementation, what if the users have a multiple devices? – Haunter Sep 20 '16 at 08:18
-
in this case use device IMEI number. store IMEI number on your database and check for that instead of user id – V-rund Puro-hit Sep 20 '16 at 08:21
-
I tried implemented it. and work fine. but this may raise some privacy issue i guess. – Haunter Sep 20 '16 at 09:30
-
yes. that is true. but many developers use it.. if you are really concern about user's privacy. you can use `ANDROID_ID`. refer [this](http://stackoverflow.com/a/3976800/5148289) and [this](http://stackoverflow.com/a/16015765/5148289). – V-rund Puro-hit Sep 20 '16 at 09:56
Some changes on my code: (Might help someone)
I have remove IMEI to avoid issues. In order for the server to validate all users registration
id to be valid, even he/she reinstalls the app. When an android device registers it new registration id on the server. After saving the new registration id,server will run a worker, starts gathering all the registration id of the current user,then send a dummy notification (empty string) on each registration id, when gcm respond with a canonical Id > 0 or an error like NotRegistered, I then delete the registration id,

- 39
- 4