'Cannot resolve symbol 'FirebaseInstanceId' Android

I started to encounter this error when I upgraded the version of the project to androidx. I also upgraded the firebase sdks. I started getting error "Cannot resolve symbol 'FirebaseInstanceId' " . With this method, I was taking the user's token and printing it to the database. How can I solve this problem?

Manifest :

<service
            android:name=".MyFirebaseIdService"
            android:enabled="true"
            android:exported="true">
            <intent-filter>
                <action android:name="com.google.firebase.INSTANCE_ID_SERVICE" />
            </intent-filter>

MyFirebaseIdService;

import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.iid.FirebaseInstanceId;
import com.google.firebase.iid.FirebaseInstanceIdService;


public class MyFirebaseIdService extends FirebaseInstanceIdService {



    @Override
    public void onTokenRefresh() {
        super.onTokenRefresh();
        FirebaseUser firebaseUser = FirebaseAuth.getInstance().getCurrentUser();

        String refreshToken = FirebaseInstanceId.getInstance().getToken();
        if (firebaseUser != null){
            updateToken(refreshToken);
        }
    }


    private void updateToken(String refreshToken) {
        FirebaseUser firebaseUser = FirebaseAuth.getInstance().getCurrentUser();

        DatabaseReference reference = FirebaseDatabase.getInstance().getReference("Tokens");
        Token token = new Token(refreshToken);
        reference.child(firebaseUser.getUid()).setValue(token);
    }
}

Token ( model class ):

public class Token {

    private String token;

    public Token(String token) {
        this.token = token;
    }

    public Token() {
    }

    public String getToken() {
        return token;
    }

    public void setToken(String token) {
        this.token = token;
    }
}

I was rewriting the token code every time the user entered the home page.

MainActiviy :

 updateToken(FirebaseInstanceId.getInstance().getToken());


   private void updateToken(String token){
        DatabaseReference reference = FirebaseDatabase.getInstance().getReference("Tokens");
        Token token1 = new Token(token);
        reference.child(fuser.getUid()).setValue(token1);
    }

build.gradle ( project )

buildscript {
    
    repositories {
        google()
        jcenter()

    }
    dependencies {      
          classpath 'com.android.tools.build:gradle:7.1.2'       
           classpath 'com.google.gms:google-services:4.3.10'
        classpath 'com.google.firebase:firebase-crashlytics-gradle:2.5.2'
        
    }
}

allprojects {
    repositories {
        google()
        jcenter()
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

build.gradle ( module )

apply plugin: 'com.android.application'
apply plugin: 'com.google.gms.google-services'
 apply plugin: 'com.google.firebase.crashlytics'



android {
    compileSdkVersion 30
    defaultConfig {
        applicationId "***"
        minSdkVersion 19
        targetSdkVersion 30
        versionCode 21
        versionName "1.1.9"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        multiDexEnabled true
    }
    lintOptions {
        checkReleaseBuilds false
        abortOnError false
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:28.0.0'
    implementation 'com.android.support:design:28.0.0'
    implementation 'com.android.support.constraint:constraint-layout:1.1.3'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
    implementation 'com.firebaseui:firebase-ui-database:4.0.1'
    implementation 'com.google.firebase:firebase-database:16.0.1'
    implementation 'com.google.firebase:firebase-storage:16.0.1'
    implementation 'com.android.support:cardview-v7:28.0.0'
    implementation 'com.android.support:recyclerview-v7:28.0.0'
    implementation 'com.android.support:design:28.0.0'
    implementation 'com.github.orangegangsters:swipy:1.2.3@aar'
    implementation 'de.hdodenhof:circleimageview:2.2.0'
    implementation 'com.theartofdev.edmodo:android-image-cropper:2.7.+'
    implementation 'com.squareup.picasso:picasso:2.5.2'
    implementation 'com.google.firebase:firebase-storage:16.0.1'
    implementation 'com.firebaseui:firebase-ui-database:4.0.1'
    implementation 'com.android.support.constraint:constraint-layout:1.1.3'
    implementation 'com.google.firebase:firebase-inappmessaging-display:17.0.1'
    implementation 'com.android.support:multidex:1.0.3'
    implementation 'com.google.android.gms:play-services-ads:15.0.1'
    implementation('com.google.apis:google-api-services-people:v1-rev2-1.21.0')
            {
                exclude module: 'guava-jdk5'
            }

    implementation 'com.squareup.retrofit2:retrofit:2.3.0'
    implementation 'com.squareup.retrofit2:converter-gson:2.3.0'
    implementation 'com.google.firebase:firebase-ads:15.0.1'
    implementation "io.grpc:grpc-okhttp:1.32.2"
    implementation 'com.android.billingclient:billing:3.0.0'


       implementation platform('com.google.firebase:firebase-bom:29.3.0')


     implementation 'com.google.firebase:firebase-crashlytics'
     implementation 'com.google.firebase:firebase-analytics'


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

    implementation 'com.google.firebase:firebase-messaging:23.0.2'
    implementation 'com.google.firebase:firebase-auth:21.0.3' 


}

apply plugin: 'com.google.gms.google-services'


----- UPDATE ---- SOLUTİON :

   FirebaseMessaging.getInstance ().getToken ()
                .addOnCompleteListener ( task -> {
                    if (!task.isSuccessful ()) {

                        return;
                    }
                    if (null != task.getResult ()) {

                        String firebaseMessagingToken = Objects.requireNonNull ( task.getResult () );

                        updateToken(firebaseMessagingToken);
                    }
                } );

  private void updateToken(String firebaseMessagingToken) {
        FirebaseUser firebaseUser = FirebaseAuth.getInstance().getCurrentUser();

        DatabaseReference reference = FirebaseDatabase.getInstance().getReference("Tokens");
        Token token = new Token(firebaseMessagingToken);
        reference.child(firebaseUser.getUid()).setValue(token);
    }



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source