I'm trying to implement a notification system in which you can subscribe to another user (not Firebase topics) and get push notifications whenever that user posts something.
To do this, I decided to use Device Groups and registration tokens so that the user can get push notifications to every device he/she is logged into.
Currently, the user can subscribe to another user and is also able to receive push notifications to every device he's logged into using the notification key. To do this, I get the registration token for the device every time the user logs in and store it in the database (App server) using the following structure.
User
- user_id (PK)
- notification_key_name
- notification_key
Device
- user_id (FK)
- registration_token
But, I'm having trouble figuring out how to manage token refreshes in the App Server.
Consider this case:
1) user_1 logs in for the first time in device.
2) A device group is created with registration_token_1 (both in FCM and in App Server).
3) user_1 uninstalls the app and reinstalls it (token is refreshed).
4) user_1 logs back in.
5) App Server adds refresh_token_1 to user_1, he now has 2 tokens, the old and the new.
6) user_1 logs out and user_2 logs in the same device, refresh_token_1 is transferred to user_2.
7) FCM deletes user_1's device group since user_1 no longer has any registration tokens. But user_1 still has notification_key and a device with registration_token_1 in the App Server.
8) user_1 is unable to login since the App Server thinks he already has a device group and tries to add refresh_token_1 to a notification_key that doesn't exist in FCM, getting error code 400 "notification key does not exist".
I have a couple questions regarding this:
If a token is refreshed, is there a way to retrieve the old token from FCM? This way the App Server can replace the old token with the new one without having to pair the token to a device identifier, which seems unreliable if the identifier does not live through uninstalls/factory resets, etc
Can you recommend a better way for mapping users (notification keys) with registration tokens in the App Server?
EDIT I've read around some other threads and found a possible solution in the form of pairing registration tokens to a device identifier, so when the token is refreshed, I can look for the device identifier and replace/remove the token. Now, I'm not sure BOTH iOS and Android have persistent identifiers that can exist beyond app uninstalls, data wipes and factory resets...