I'm pretty new to Firebase Cloud Messaging, and I haven't been able to get a satisfactory answer to this question yet (also, sorry for. all the full. stops, unfortunately I bought a macbook. in. 2017, the year apple decided to experiment. with its keyboard - spoiler alert, bad idea - and the. space key is. broken). When a user logs in on a new device, I retrieve the device token and add it to that user's device group. In any event, I. am. using. Firebase. Cloud Messaging. (FCM) to send messages between users. When. the user. logs out, I. remove. that device token from that. user's device group. My question. is, if the token expires and I. need to add. the new token to the device group, do I. have to. remove the old token, or does FCM automatically remove expired tokens/tokens that. are no longer valid for whatever reason from. the device group, or will they just pile up and need to be removed manually (i.e. by me). Thanks!
Asked
Active
Viewed 2,907 times
-1
-
https://stackoverflow.com/questions/38079378/firebase-cloud-messaging-device-groups-leak – AskNilesh Aug 14 '18 at 08:02
-
It is the developer's responsibility to keep track of the tokens in the device groups that they have. – AL. Aug 14 '18 at 14:23
2 Answers
3
As far as I know tokens are not automatically removed from a device group when they expire. You will have to do that in your app server code.
If you have the old token, you can do that immediately when the token is refreshed.
Alternatively you can detect outdated/expired tokens when sending a message, and remove them then. For an example of this, see this code that sends messages using the Admin SDK.

Frank van Puffelen
- 565,676
- 79
- 828
- 807
-
Ok, but here's the thing. When I send a test message to an old (invalid) device token, I receive the following response: "The provided registration token is not registered. A previously valid registration token can be unregistered for a variety of reasons. See the error documentation for more details. Remove this registration token and stop using it to send messages." So it seems like FCM is automatically removing old, useless tokens from the device group without my intervention. Is this accurate? – gollyzoom Aug 14 '18 at 20:41
-
How to manage this with multiple devices? From mobile apps I can pass the unique ID of the mobile device but what is the appropriate "device id" for web apps? How does firebase uniquely identify the browser's service worker? I would need that to manage the tokens on my server wouldn't I? – crowmagnumb Oct 14 '19 at 19:35
-
Oh, and what if the mobile device is simply discarded, how would we ever know and thus delete it from our server? I'm thinking I'll record a timestamp with every notification of a token to our server and then if some deviceid was not updated in a very long time eventually just remove it? – crowmagnumb Oct 14 '19 at 20:43
0
The registration token may change when:
- The app deletes Instance ID
- The app is restored on a new device
- The user uninstalls/reinstall the app
- The user clears app data.
Previous token become invalid after generation of new token, good approach is to update access token on each login and remove token on each logout, by doing this you will always get the updated token. For more understading
https://firebase.google.com/docs/cloud-messaging/android/client#sample-register

AskNilesh
- 67,701
- 16
- 123
- 163

Roshaan Farrukh
- 399
- 3
- 10