18

We've been working on a GCM implementation and have noticed that a device address assigned to a an app installation, can live on even if the app is uninstalled.

So we install an app, get token A, device subscribes to a particular alert type 1, message token A with great success. Then we uninstall the app.

No we reinstall, receive token B, and the device subscribes to a particular alert type 2, we message token B with great success.

Now since we didn't send a message to token A between the time the app was uninstalled and reinstalled, we can still message both tokens, and the app receives them both.

Had we tried to message token A while the app was uninstalled, we could have cleaned that up from Google's response.

Is there any way to know that token A is technically no longer valid?

2 Answers 2

13

From the official documentation:

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:

  1. The end user uninstalls the client app.
  2. The app server sends a message to GCM connection server.
  3. The GCM connection server sends the message to the GCM client on the device.
  4. 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.
  5. The GCM client on the device informs the GCM connection server that the client app was uninstalled.
  6. The GCM connection server marks the registration token for deletion.
  7. The app server sends a message to GCM.
  8. The GCM returns a NotRegistered error message to the app server.
  9. 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.

However, it can apparently happen that you still get the notification for the old registration ID, as users state in other questions:

For this problem, there is a functionality called "canonical IDs":

Canonical IDs

If a bug in the client app triggers multiple registrations for the same device, it can be hard to reconcile state and the client app might end up with duplicate messages.

Implementing canonical IDs can help you more easily recover from these situations. A canonical registration ID is the registration token of the last registration requested by the client app. This is the ID that the server should use when sending messages to the device.

If you try to send a message using an old registration token, GCM will process the request as usual, but it will include the canonical ID in the registration_id field of the response. Make sure to replace the registration token stored in your server with this canonical ID, as eventually the old registration token will stop working.

1
  • 1
    Interestingly, we have a very large discrepancy between devices that are considered by GCM as active, but for which there was no any user activity for more than a year. Also, the number of active devices in the Google Play dashboard is significantly lower than the number of devices to which GCM messages are sent and no failure is reported.
    – silentser
    Commented Apr 6, 2017 at 11:27
6

I'm assuming that by 'token' you are actually referring to the registration ID. Old registration IDs can remain active for a while. However Google tells you that you need to update your regid for a particular device/app combination by means of the canonical ID in the response to your sent message.

2
  • thanks @nickT, storing that worked perfectly. "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." Commented Dec 24, 2013 at 5:01
  • Just to mention something; You will get the same registration ID for same device, after Google's gcm server update.
    – EvanBlack
    Commented Mar 5, 2014 at 18:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.