Mahdi-Malv
Mahdi-Malv

Reputation: 19250

Firebase instance id: deprecation of getId() in 21.0.0

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?

Upvotes: 24

Views: 23266

Answers (4)

CopsOnRoad
CopsOnRoad

Reputation: 268024

  • 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();
    

Upvotes: 4

Mahdi-Malv
Mahdi-Malv

Reputation: 19250

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()

Upvotes: 10

Adnan Bashir
Adnan Bashir

Reputation: 763

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;
    }

Upvotes: 0

Midhun Murali
Midhun Murali

Reputation: 946

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.

Upvotes: 49

Related Questions