Ruslan Leshchenko
Ruslan Leshchenko

Reputation: 4253

Firebase database better way to save FCM tokens to avoid duplicates

I need to save FCM tokens to Firebase Database to send notifications. Is there a better way to do it? Now I do it like this:

FirebaseDatabase.getInstance().reference.child("users/${user.uid}/tokens").push().setValue(token)

This way, each time when the token is pushed, it has new unique id and can have same token.

Could you please give me an advice? Or maybe I can do it with firebase functions which will remove duplicates.

Upvotes: 6

Views: 8303

Answers (2)

hifromantal
hifromantal

Reputation: 1764

I would advise against saving that information in a database.

Take in consideration that:

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.

Source

FirebaseInstanceId may be considered PID (personal identification data), so treat it accordingly - remember the new GDPR regulations kicking in March 2018 if you do business in the EU

Instead, I would just use the token to register the user for push notifications where needed and send it to your FCM/GCM server.

Upvotes: 4

Ruslan Leshchenko
Ruslan Leshchenko

Reputation: 4253

I solved this problem by using another way for saving tokens, now token saves like a key of the record.

FirebaseDatabase.getInstance().reference.child("users/${user.uid}/tokens").child(token).setValue(true)

Upvotes: 10

Related Questions