Unique Firebase Instance-Id for every user account

旧街凉风 提交于 2020-07-09 14:32:28

问题


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.


回答1:


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.




回答2:


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




回答3:


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");


来源:https://stackoverflow.com/questions/55628571/unique-firebase-instance-id-for-every-user-account

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!