27

I just did an upgrade to all my Firebase dependencies and I have an issue, FirebaseInstanceId is not recognized anymore. I have used it to get the token id like this:

String tokenId = FirebaseInstanceId.getInstance().getToken();

This is the error:

Cannot resolve symbol 'FirebaseInstanceId'

Here is my build.gradle file:

//Firebase
implementation 'com.google.firebase:firebase-auth:16.0.1'
implementation 'com.google.firebase:firebase-database:16.0.1'
implementation 'com.google.firebase:firebase-firestore:17.0.1'

//FirebaseUI
implementation 'com.firebaseui:firebase-ui-auth:4.0.0'
implementation 'com.firebaseui:firebase-ui-database:4.0.0'
implementation 'com.firebaseui:firebase-ui-firestore:4.0.0'

How can I get the token id using the latest dependencies?

Peter Haddad
  • 78,874
  • 25
  • 140
  • 134
Joan P.
  • 2,368
  • 6
  • 30
  • 63

6 Answers6

35

I also faced the same problem. From the doc, it says FirebaseInstanceId is no longer available and says to use FirebaseMessaging.getInstance().token as below

FirebaseMessaging.getInstance().token.addOnCompleteListener(OnCompleteListener { task ->
    if (!task.isSuccessful) {
        Log.w(TAG, "Fetching FCM registration token failed", task.exception)
        return@OnCompleteListener
    }

    // Get new FCM registration token
    val token = task.result
})
Soe Thiha
  • 521
  • 1
  • 5
  • 6
14

add the following to the build.gradle file:

 implementation 'com.google.firebase:firebase-messaging:17.0.0'
 implementation 'com.google.firebase:firebase-core:16.0.0'

more info here:

https://firebase.google.com/docs/cloud-messaging/android/client#set-up-firebase-and-the-fcm-sdk

Peter Haddad
  • 78,874
  • 25
  • 140
  • 134
  • This is no longer an option because Firebase BoM already sets its own versions for all firebase libraries to ensure mutual compatibility. – andreszs Aug 30 '23 at 15:13
9

Add into Gradle:

  implementation 'com.google.firebase:firebase-messaging:22.0.0'

  implementation 'com.google.firebase:firebase-core:19.0.0'

Then add the following code to get the token:

public static String returnMeFCMtoken() {
        final String[] token = {""};
        FirebaseMessaging.getInstance().getToken().addOnCompleteListener(new OnCompleteListener<String>() {
            @Override
            public void onComplete(@NonNull Task<String> task) {
                if(task.isComplete()){
                    token[0] = task.getResult();
                    Log.e("AppConstants", "onComplete: new Token got: "+token[0] );

                }
            }
        });
        return token[0];
    }
Clean Coder
  • 496
  • 5
  • 12
  • 2
    FirebaseMessaging.getInstance().getToken() is an asynchronous call, so return token[0] would return "". you need a way to wait for the response – Eshiet Jul 12 '21 at 10:28
  • This is no longer an option because Firebase BoM already sets its own versions for all firebase libraries to ensure mutual compatibility. – andreszs Aug 30 '23 at 15:14
6

I was also facing the same problem. Actually, FirebaseInstanceId has been shut down, replaced with Firebase Installation.

Try:

FirebaseInstallations.getToken();
Şafak Gezer
  • 3,928
  • 3
  • 47
  • 49
Pravin Gupta
  • 69
  • 1
  • 3
0

try this:

Add these dependencies to your gradle file:

implementation 'com.google.firebase:firebase-messaging:23.0.5'
implementation 'com.google.firebase:firebase-core:21.0.0'

Get firebase instance with FirabaseMessaging

String deviceToken = FirebaseMessaging.getInstance().getToken().toString();
Mohamed Rejeb
  • 2,281
  • 1
  • 7
  • 16
micoder
  • 1
  • 2
0
fun setNewFcm() {
    FirebaseMessaging.getInstance().token.addOnCompleteListener { task ->
        if (!task.isSuccessful) {
            return@addOnCompleteListener
        }
        if (task.result != null) {
            val token: String = task.result
            AppSharedData.setFcmToken(token)
        }
    }
}

Or by using this

fun setNewFcm() {
    FirebaseInstallations.getInstance().getToken(true)
        .addOnCompleteListener { task ->
            if (!task.isSuccessful) {
                return@addOnCompleteListener
            }
            if (task.result != null) {
                val token: String = task.result.token
                AppSharedData.setFcmToken(token)
            }
        }
}