简体   繁体   中英

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.

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 .

If you want to have Unique FCM Instance Id for each user on a device, you should call FirebaseInstanceId.getInstance().deleteInstanceId() on each logout on an account. Then in next FirebaseInstanceId.getInstance().instanceId call, you'll get a new unique Intance Id

But there is a point that you need to know. Call deleteInstanceId() on a new Thread , not MainThread

The best way to handle your issue, is to use topic messaging.

From the docs :

Based on the publish/subscribe model, FCM topic messaging allows you to send a message to multiple devices that have opted in to a particular topic. You compose topic messages as needed, and FCM handles routing and delivering the message reliably to the right devices.

When the user logs in the app, you can subscribe to a certain topic, for example:

FirebaseMessaging.getInstance().subscribeToTopic("weather")
    .addOnCompleteListener(new OnCompleteListener<Void>() {
        @Override
        public void onComplete(@NonNull Task<Void> task) {
            String msg = getString(R.string.msg_subscribed);
            if (!task.isSuccessful()) {
                msg = getString(R.string.msg_subscribe_failed);
            }
            Log.d(TAG, msg);
            Toast.makeText(MainActivity.this, msg, Toast.LENGTH_SHORT).show();
        }
    });

Then the user can receive notifications based on the topic name. Then when the user clicks the logs out button, you can call the following:

FirebaseMessaging.getInstance().unsubscribeFromTopic("weather");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM