android unable to retrieve the device token FirebaseInstanceId.getInstance() is deprecated

182 views Asked by At

Android Studio SDK 30 Gradle 7

I'm able to receive notification.

but unable to get the device token :(


all answers on the web talks about FirebaseInstanceId.getInstance is deprecated and answers with a FirebaseInstanceId.getInstance code :(

Android don't let me use it

I the androidManifest I do have

<service
    android:name=".MyFirebaseMessagingService"
    android:exported="false"
    android:stopWithTask="false">
    <intent-filter>
        <action android:name="com.google.firebase.MESSAGING_EVENT"/>
    </intent-filter>
</service>

in build.gradle I have

   classpath 'com.google.gms:google-services:4.3.5'

and at module level

dependencies {

    implementation 'androidx.appcompat:appcompat:1.2.0'
    implementation 'com.google.android.material:material:1.3.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
    implementation 'com.google.firebase:firebase-messaging:21.0.1'
    implementation 'com.google.firebase:firebase-core:18.0.2'
    implementation 'com.google.firebase:firebase-analytics'
    implementation platform('com.google.firebase:firebase-bom:26.5.0')
    testImplementation 'junit:junit:4.13.2'
    androidTestImplementation 'androidx.test.ext:junit:1.1.2'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
}

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

in a created class

public class MyFirebaseMessagingService extends FirebaseMessagingService {

I have

@Override
public void onNewToken(@NonNull String token) {
    super.onNewToken(token);
    Log.e("newToken", token);
    //Add your token in your sharepreferences.
    getSharedPreferences("_", MODE_PRIVATE).edit().putString("fcm_token", token).apply();
}

but this onNewToken never triggered, the log cat never shows the "newToken" :(

is someone knows how to fix this ?

1

There are 1 answers

0
Jintor On

Finally,

I found the right documentation

https://firebase.google.com/docs/cloud-messaging/android/client