3

We're using Firebase Cloud Messaging for delivering push notifications.

Recently, I started seeing that the push notifications are delivered twice to the same device.

When I dug deeper, I found out that in our backend one device is registered with 2 firebase tokens. And both of them are working. Therefore, the device token provided by iOS must not have changed.

As per GCM documentation, this could happen due to a bug in the client:

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.

So in case that we send a push notification to the old token, we should get a response with registration_id key value pair in it.

FCM documentation describes this case in more detail:

If message_id is set, check for registration_id:

If registration_id is set, replace the original ID with the new value (canonical ID) in your server database. Note that the original ID is not part of the result, so you need to obtain it from the list of registration_ids passed in the request (using the same index).

However, there's no registration_id in the response when using any of the two working tokens. So it seems FCM is not aware that the device is registered with multiple tokens and therefore does not include registration_id in the response.

The response looks like this:

{
    "multicast_id": 9087190295926530487,
    "success": 1,
    "failure": 0,
    "canonical_ids": 0,
    "results": [{
        "message_id": "0:1498726709849471%10726fcd10726fcd"
    }]
}

My question is: Is there any way how to get canonical ids for all devices that are registered in FCM and have multiple tokens? Or what would be the recommended way to fixing a problem like this?

Community
  • 1
  • 1
Tom Kraina
  • 3,569
  • 1
  • 38
  • 58
  • Odd. Would you also be able to identify which of the tokens is more recent? Do you also have multiple senders? – AL. Jun 29 '17 at 09:15
  • I'm not sure what you mean by multiple senders? But yes, I can identify which one is more recent. So we could run through our db and remove the olds ones for users who have more tokens. However, this is not very systematic as the deletion would need to be run periodically. – Tom Kraina Jun 29 '17 at 09:23
  • Sorry, I was asking if you have enabled multiple projects to send to your app (see my answer [here](https://stackoverflow.com/q/42117592/4625829)). On the top of my mind, I think this is already an unexpected behavior. Since there should only be a single registration token valid, if there is another one, it shouldn't take too long for it to be invalidated (triggering *Token Refresh*) -- hence I thought maybe this is a registration token for a different sender. – AL. Jun 29 '17 at 09:28

0 Answers0