4

I create a small Android Application which should create unique userIds.

I was thinking about to use FirebaseInstanceId.getId() for that because the documentation says

Returns a stable identifier that uniquely identifies the app instance

But the what does app mean in this context?

Does app mean the "real" Android or iOS App which uses the FirebaseInstanceId? Or does it mean the "Firebase App" or better the Firebase Project?

Can I assume that the generated id is unique in my Firebase Project and can be stored in FirebaseDatabase as userId or should I create a unique userId in another way?

StefMa
  • 3,344
  • 4
  • 27
  • 48

2 Answers2

6

You can infer that the "app" in this case means a particular app installation on a device. Look at the javadocs for FirebaseInstanceId, and take note of the rules for when an instance id can change. Install/uninstall and clearing app data are a result of user behavior against an app on their device. Note that this value can change, and therefore probably isn't appropriate for uses outside of Firebase.

This value is used by Firebase for identifying an app that could receive a cloud message. It does not represent a particular user. A user could have the same APK installed on multiple devices, using a single account to log into each one of them. If you want to send that user a cloud message, which instance of the app should it go to, on which device? You probably want to send to all of them. That's where instance id comes into play. In order to preserve consistent behavior of your app, no matter how many times it's been installed for a user, you need the instance id of each unique installation in order to target a message to all of them.

To identify a particular user, you should use Firebase Authentication, and assume that they're going to log into the same account every time, so there will be a single UID that you can store to refer to that one person. It's common to associate multiple instance ids to a single user, for the purpose of targeted messaging to that user.

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
1

I'm using 2 different method on 2 different apps,

The first one: use device IMEI (you need to ask for READ_PHONE_STATE permission to do that),

Then, your Firebase structure will be something like -

/uniqueImei1/Database1..(games/notes etc..)
/uniqueImei2/Database2..(games/notes etc..)

OR, the BETTER way to do that is to use Firebase Auth UID, Each user, once he connect to your app using Name&Pass/Google login/Facebook login etc.. gets an unique id called UID, to achieve that UID you will use

String UID = FirebaseAuth.getInstance().getCurrentUser().getUid();

then, your firebase structure -

/UID1/Database1..(games/notes etc..)
/UID2/Database2..(games/notes etc..)

For more information about option 2, Click here.

You are really should use option 2, Good luck :)

Community
  • 1
  • 1
Nirel
  • 1,855
  • 1
  • 15
  • 26
  • Thank you. Probably that is a better (the best?) solution. But it doesn't answer my question :) – StefMa Jan 12 '17 at 18:31