24

With the recent release of FirebaseInstanceId and FirebaseCloudMessaging (21.0.0) Firebase has deprecated iid package and both getToken() and getId() methods are now deprecated.

According to the Firebase release note the method getToken() is moved to FirebaseMessaging

Before:

FirebaseInstanceId.getInstance().getToken()

After:

FirebaseMessaging.getInstance().getToken()

Which gives use the fcmToken, but to retrieve instance id, there's no method available in FirebaseMessaging nor FirebaseInstanceId.


So, Is instance_id considered a useless id and should no longer be used? or is there a replacement for this?

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
Mahdi-Malv
  • 16,677
  • 10
  • 70
  • 117

4 Answers4

49

FirebaseInstanceId class is deprecated, to get token use FirebaseMessagingClass. The token can be generated using the following code:

FirebaseMessaging.getInstance().getToken()
.addOnCompleteListener(new OnCompleteListener<String>() {
    @Override
    public void onComplete(@NonNull Task<String> task) {
      if (!task.isSuccessful()) {
        Log.w(TAG, "Fetching FCM registration token failed", task.getException());
        return;
      }

      // Get new FCM registration token
      String token = task.getResult();

      // Log and toast
      String msg = getString(R.string.msg_token_fmt, token);
      Log.d(TAG, msg);
      Toast.makeText(MainActivity.this, msg, Toast.LENGTH_SHORT).show();
    }
});

Regarding the Firebase InstanceId, this is what the official document says:

public Task getInstanceId () -> This method is deprecated. For an instance identifier, use FirebaseInstallations.getId() instead. For an FCM registration token, use FirebaseMessaging.getToken() instead.

Midhun Murali
  • 921
  • 6
  • 11
10

Fcm Token

Before deprecation

val fcmToken = FirebaseInstanceId.getInstance().getToken()

Replacement

val fcmToken = FirebaseMessaging.getInstance().getToken()

FirebaseInstanceId#getId

Before deprecation

val istanceId = FirebaseInstanceId.getInstance().getId()

Replacement

Checking out the code of FirebaseInstanceId#getId() I saw the suggestion that you should use FirebaseInstallations#getId instead.

This method is deprecated

Use FirebaseInstallations.getId() instead.

val instanceId = FirebaseInstallation.getInstance().getId()
Mahdi-Malv
  • 16,677
  • 10
  • 70
  • 117
  • 2
    Note that this may not return the same thing. FirebaseInstallation instanceId is not the one that's meant to be used for FCM – mliu Dec 09 '20 at 04:04
  • 1
    FirebaseMessaging.getInstance().getToken() gave me the same result as the deprecated FirebaseInstanceId.getInstance().getToken() – mliu Dec 09 '20 at 23:56
  • That is `fcmToken`. The question is about instanceId and not token. InstanceId can be achieved using `FirebaseInstanceId#getId()` method. – Mahdi-Malv Dec 10 '20 at 10:42
4
  • FCM Token:

    Use firebase_messaging package

    String? token = await FirebaseMessaging.instance.getToken();
    
  • Installation ID:

    Use flutterfire_installations package

    String id = await FirebaseInstallations.instance.getId();
    
  • Installation auth token:

    String token = await FirebaseInstallations.instance.getToken();
    
CopsOnRoad
  • 237,138
  • 77
  • 654
  • 440
0

try this one

 public String getToken() {
String token;
        FirebaseMessaging.getInstance().getToken()
                .addOnCompleteListener(new OnCompleteListener<String>() {
                    @Override
                    public void onComplete(@NonNull Task<String> task) {
                        if (task.isSuccessful()) {

                           token = task.getResult();
                            

                        }
                    }
                });
        
        return token;
    }
Adnan Bashir
  • 645
  • 4
  • 8
  • 1
    This'll return FCM token which is the `result.token` of `iid.getToken()`. There is a `FirebaseInstanceId.id` which is now achieved by `FIS.getId()` according to the accepted answer – Mahdi-Malv Jan 07 '21 at 15:15