Ad

Unique Firebase Instance-Id For Every User Account

I'm unsure how to handle different user accounts on the same device appropriately, as Firebase only creates one Instance-Id per device.

Therefore, I though that it would be possible to delete the Instance-Id when the user is logged out and to create a new one when a new user is logged in.

On login:

FirebaseInstanceId.getInstance().getInstanceId().addOnCompleteListener(task -> System.out.println(task.getResult().getToken()));

On logout:

FirebaseInstanceId.getInstance().deleteInstanceId()

Does Firebase ensure that the Instance-Id will be unique, even if it is generated multiple times on the same device? The reasons why I prefer this approach are that it's simple to unsubscribe the user from all topics at once and moreover push notifications can be addressed to a specific user.

Ad

Answer

Does Firebase ensure that the Instance-Id will be unique, even if it is generated multiple times on the same device?

Regenerating an Instance ID will indeed always result in a unique value. As long as you ensure that you delete the Instance ID when the user logs out, you'll get a fresh token next time around.

To ensure your token registry (the place where you store tokens) doesn't accumulate too many outdated tokens, be sure to either remove the token when the user signs out, or when you find out a token is no longer valid when sending messages. See my answer to this question.

Ad
source: stackoverflow.com
Ad