While working with Firebase and Device Groups, I was testing the case in which the token (registration id) changes (for example after reinstalling the app) but my logic failed because after reinstalling the app i have no more way to know the previously stored token .
The problem is that now I have a device group with two "ghost"
registration ids that I don't know anymore because they changed.
I know the group get removed after every member has been unregistered but i don't know the registration ids to unregister them manually.
What can I do?
Is there a way to retrieve the members of a device group or deleting it at once?
Also, what is a good way to manage the case in which the token gets refreshed ?
I know the group get removed after every member has been unregistered but I don't know the registration ids to unregister them manually. What can I do?
When you send a message to a device group, you'll receive a response that contains a success
and failure
parameters. 0
value for `failure means that the messages we're queued properly in the FCM servers.
It there were failed messages, the list of registration tokens that failed will be in the response, where the advise is to retry sending the message. Referring to the docs:
Sending downstream messages to device groups
Sending messages to a device group is very similar to sending messages to an individual device. Set the to parameter to the unique notification key for the device group. See Message types for details on payload support. Examples in this page show how to send data messages to device groups in HTTP and XMPP protocols.
HTTP POST Request
https://fcm.googleapis.com/fcm/send Content-Type:application/json Authorization:key=AIzaSyZ-1u...0GBYzPu7Udno5aA { "to": "aUniqueKey", "data": { "hello": "This is a Firebase Cloud Messaging Device Group Message!", } }
HTTP Response
Here is an example of "success"— the
notification_key
has 2 registration tokens associated with it, and the message was successfully sent to both of them:{ "success": 2, "failure": 0 }
Here is an example of "partial success" — the
notification_key
has 3 registration tokens associated with it. The message was successfully sent to 1 of the registration tokens only. The response message lists the registration tokens that failed to receive the message:{ "success":1, "failure":2, "failed_registration_ids":[ "regId1", "regId2" ] }
When a message fails to be delivered to one or more of the registration tokens associated with a
notification_key
, the app server should retry with backoff between retries.
However, before you perform a retry, you could first verify if the FCM token is still valid (either by using dry_run
or the Instance ID Server API). If it shows that the device is no longer valid (usually NotRegistered
, you should then remove that token from the device group and from your App Server (or move it to a trash logs or something).
Also, what is a good way to manage the case in which the token gets refreshed?
If the token gets refreshed (through onTokenRefresh()
), the thing to do is to find the old token the user has and replace it with the new one, applying the changes where it matters (which is also mapping the device groups).
Also see my answers here and here. Some details might be helpful.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With