简体   繁体   English

每个用户帐户的唯一 Firebase Instance-Id

[英]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.我不确定如何适当地处理同一设备上的不同用户帐户,因为 Firebase 只为每台设备创建一个 Instance-Id。

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.因此,我认为可以在用户注销时删除 Instance-Id,并在新用户登录时创建一个新的 Instance-Id。

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? Firebase 是否确保 Instance-Id 是唯一的,即使它是在同一台设备上多次生成的? 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? Firebase 是否确保 Instance-Id 是唯一的,即使它是在同一台设备上多次生成的?

Regenerating an Instance ID will indeed always result in a unique value.重新生成一个实例 ID 确实总是会产生一个唯一的值。 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.只要您确保在用户注销时删除实例 ID,下次您就会获得一个新令牌。

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.如果您希望设备上的每个用户都有唯一的 FCM 实例 ID,则应在每次注销帐户时调用FirebaseInstanceId.getInstance().deleteInstanceId() Then in next FirebaseInstanceId.getInstance().instanceId call, you'll get a new unique Intance Id然后在下一个FirebaseInstanceId.getInstance().instanceId调用中,您将获得一个新的唯一实例 ID

But there is a point that you need to know.但是有一点你需要知道。 Call deleteInstanceId() on a new Thread , not MainThread在新 Thread 而不是 MainThread 上调用 deleteInstanceId()

The best way to handle your issue, is to use topic messaging.处理您的问题的最佳方法是使用topic消息传递。

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.基于发布/订阅模型,FCM 主题消息传递允许您向已选择加入特定主题的多个设备发送消息。 You compose topic messages as needed, and FCM handles routing and delivering the message reliably to the right devices.您可以根据需要撰写主题消息,FCM 负责处理路由并将消息可靠地传递到正确的设备。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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