I would like to find a sustainable solution for storing FCM tokens used for push notifications. I am using React Native with Firebase.
The requirements are:
- User-based application
- Multiple devices per user supported
- No stale tokens stored at any time.
My current approach is:
Maintain a table user_devices with columns:
- user_id,
- device_uuid
- fcm_token
Maintain a uniqueness constraint on fcm_token and device_uuid.
Upon key events, including but not limited to:
- application startup
- sign in
- onTokenRefresh
send the following to the backend:
- device unique id
- FCM registration token
- user id, if there is one (done through authentication token)
The backend then upserts a row with the above values. If the device uuid exists, that means, we overwrite the token (and, potentially, the user_id) for that device uuid.
The above approach is flawed because:
- device unique id can change at any point (device reset, update, app reinstall, etc...)
- registration token can change at any point
In other words, the table ends up storing many stale tokens and the application backend sends notifications to those stale tokens.
Additionally, the "Devices" frontend page (which shows the user their registered devices and reads from the above table) shows many duplicate devices that, really, represent the same device but have different device uuids.
One mitigation step could be to periodically send notifications to all tokens stored in the table with dry_run=true. Then, remove rows with tokens for which an error occurred. This is bad because it implies there is a period during which tokens are stale in the table.
I have scanned multiple resources online, including FCM documentation, but there is no mention on storing and maintaining those tokens in a real-world application. Are there any suggested approaches?