I'm developing a push notification app, do you have same issue:
My app generate a FCM device token and store it in a cloud database, so I can send notification messages to a device via FCM, and my database table looks like:
userid | device_token
Mary | xxxxxxxxxxxxxxxxxxxxxxxxxxxx // token1, from Mary's first device
John | yyyyyyyyyyyyyyyyyyyyyyyyyyyy
Mary | zzzzzzzzzzzzzzzzzzzzzzzzzzzz // token2, from Mary's second device Mary | kkkkkkkkkkkkkkkkkkkkkkkkkkkk // token, from Mary's first device .......
After Mary reinstalled this app from her first device, a new device token generated, and it is stored with token3.
How can I remove the expired device token token1, the only information I got may only be a pair of device token and an account name.
So how do you manage your device in this situation?
If "Mary" is using the same account to log in each time in your app, even if it is a new phone or reinstalled app why do you create a new token field inside the database? Why don't you always write inside the same token field so you always have access to this field. This will also send notifications only to the phone that your user is actually using right now. So each time when the user starts the app check token, if not equal write the new one inside your database. And from the server-side take those tokens and send notifications.
Am I missing something?
To do this, I would suggest using FirebaseAuth for the SignIn and SignUp process of your application. Then use generated
uid
as the field ID for the user inside the Realtime Database. You can get thisuid
withFirebaseAuth.getInstance().getCurrentUser().getUid()
. So your user Mary will always have the sameuid
no matter what phone she uses. Always find the user inside the database with thisuid
and overwrite the existing Firebase token. This also means that your "users" will not be a single line field inside the database, but a more complex and better representation of a user. You can use model classes for this like:It's up to you on how to configure this. But using models is also helping you on posting and retrieving data, like this:
Creating new user:
Retrieving data from the real-time database: