Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 935
Ruslan Leshchenko Avatar asked Dec 11 '22 08:12

Ruslan Leshchenko


2 Answers

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)
like image 58
Ruslan Leshchenko Avatar answered Apr 20 '23 01:04

Ruslan Leshchenko


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.

like image 26
anthonymonori Avatar answered Apr 20 '23 01:04

anthonymonori