0

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 thought 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.

twister21
  • 95
  • 2
  • 10

3 Answers3

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.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • Thank you for the advice. I've found the following statement: "One app instance can be subscribed to no more than 2000 topics". Do you know whether "app instance" refers to the Instance-ID of an application on a single device or to the whole project? If the first statement is true: Would it be appropriate to create a topic for each user, so that all devices on which the user is logged in can subscribe to it? Or should I send notifications to all device tokens which belong to the user account? The first approach would be much easier, as I only need a user's ID in my database. – twister21 Apr 11 '19 at 15:14
  • Creating a topic per user is not uncommon. Just keep in mind that anyone can subscribe to a topic if they know the topic's name, and that topic fan-out is outside of *your* control and may be slower than doing the fan-out yourself. – Frank van Puffelen Apr 11 '19 at 16:44
  • What do you mean by "anyone"? I think that it's necessary to know the auth key, e.g. for https://iid.googleapis.com/iid/v1:batchAdd. – twister21 Apr 11 '19 at 17:16
  • To subscribe you only need to know the topic, and configuration information that is available in your APK. – Frank van Puffelen Apr 11 '19 at 18:41
1

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

Hassan Alizadeh
  • 103
  • 1
  • 8
0

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");
Peter Haddad
  • 78,874
  • 25
  • 140
  • 134
  • you can have multiple user subscribed to one topic.. you can have 2000 users subscribed to topic a and 2000 other users subscribed to topic b.. – Peter Haddad Apr 11 '19 at 09:45