3

I'm trying to use firebase cloud messaging service for my android application and I'm trying to find the best way to manage registration ID in database server. I was thinking to create new table with userID,registrationId (where userID is unique for each user) in my database and insert new record once the user logs in successfully and remove that record when the user logs out. but there are some situation that the registration Id will be refreshed, I can get the new registration Id to save it in the database. but how can I get the old registration Id to remove it?

Are there better way to manage the registration Id in database?

note: a device can access one account but there are might be many devices that use the same account.

user9434613
  • 89
  • 2
  • 6

1 Answers1

3

Depending on the user, you might want to also have an identifier for each device they use. But for a simpler explanation, I'll go with the scenario where each user only has a single device.

If you're using Firebase Database, then the simplest way to structure the nodes would be something like this:

pushTokens/
    $userId: <registration_token_here>

Simple as that. You just pair the userId that you use in your app (possibly for authentication) and place the token there. On sign out, log the user out. When the user is currently signed-in and the token refreshes, handle it in onTokenRefresh(), send the new token to the DB, and replace the older one. Deciding to keep the old one for logging purposes is your call.

Possibly helpful posts:

AL.
  • 36,815
  • 10
  • 142
  • 281
  • 2
    Users can have multiple devices - if they subscribe through their phone, and also through their desktop - this is going to knock out the other device. You can push the new token to the $userId but then this doesn't help with expiring tokens. – sketchthat Mar 08 '18 at 00:39
  • @sketchthat That's why I mentioned "*Depending on the user, you might want to also have **an identifier for each device they use**. But for a simpler explanation, I'll go with the scenario where each user only has a single device.*" I'm quite familiar with the multiple devices use case, for that case, they should [manage their device groups](https://stackoverflow.com/a/42522002/4625829). – AL. Mar 08 '18 at 01:50
  • Thanks @AL that link is really helpful. – sketchthat Mar 09 '18 at 02:44