6

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.

anthonymonori
  • 1,754
  • 14
  • 36
Ruslan Leshchenko
  • 4,043
  • 4
  • 24
  • 36

2 Answers2

10

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)
Ruslan Leshchenko
  • 4,043
  • 4
  • 24
  • 36
  • Could you expand upon your solution a bit more? Why did this approach helped solve your initial problem? – anthonymonori Jan 03 '18 at 13:15
  • @anthonymonori In case of using this approach Firebase Database doesn't create a new record in the database if token the same. And I shouldn't do anything to remove duplicates. – Ruslan Leshchenko Jan 03 '18 at 14:14
  • 2
    This is indeed the right solution. Nodes with children of the form `"valueoftoken": true` are Firebase Database's equivalent of **set** collections. Also see http://stackoverflow.com/questions/40656589/firebase-query-if-child-of-child-contains-a-value – Frank van Puffelen Jan 03 '18 at 14:29
  • 1
    can i use mongo db instead of firebase database to store token and user info? – Sumanth Varada Jun 21 '19 at 13:49
  • @SumanthVarada Yes, you can create a rest api and simply save and fetch your tokens from mongo db. – Sean Dvir Feb 08 '20 at 20:55
  • You must do this to have onlyu one token firebaseDatabase.reference.child("users/${user!!.uid}/tokens").child("token").setValue(it) – Frank Jun 22 '21 at 16:26
4

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.

Community
  • 1
  • 1
anthonymonori
  • 1,754
  • 14
  • 36
  • Thanks for the advice. My app sends tokens during the opening of the main screen and in onTokenRefresh() method of FirebaseInstanceIdService, also after sending a notification, all invalid tokens will be deleted. How do you think is it fine? – Ruslan Leshchenko Jan 03 '18 at 13:28
  • Depends on your FCM/GCM server setup (or you maybe using Amazon SNS, but none the less, you should not care about invalid tokens - Firebase will not be able to send any notifications to those. Your only concern should be to always re-register the user if: they have been previously agreed to get push notifications and when the token is refreshed. Always make sure you get the users consent/permission before sending them anything! – anthonymonori Jan 03 '18 at 14:07
  • I understood, before sending a notification I should get user's consent. Thanks. – Ruslan Leshchenko Jan 03 '18 at 14:16