简体   繁体   中英

Firebase registration token is not showing in Android Studio logcat

I am using the Android studio Arctic Fox version. I need to implement an automated notification app that is going to send notifications by date. I am running my apps on an actual device with the 6.0.1 android version. I have connected fcm and set up the dependencies as well. But token is not showing on logcat. Also registered the app in firebase. Any ideas why this is happening?

build:gradle(:app)

plugins {
    id 'com.android.application'
    id 'com.google.gms.google-services'
}

android {
    compileSdk 31

    defaultConfig {
        applicationId "com.example.f1"
        minSdk 21
        targetSdk 31
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
}

dependencies {

    implementation 'androidx.appcompat:appcompat:1.3.1'
    implementation 'com.google.android.material:material:1.4.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.1.1'
    implementation 'com.google.firebase:firebase-messaging:22.0.0'
    testImplementation 'junit:junit:4.+'
    androidTestImplementation 'androidx.test.ext:junit:1.1.3'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
}

build:gradle(module)

  dependencies {
        classpath "com.android.tools.build:gradle:7.0.2"
        classpath 'com.google.gms:google-services:4.3.10'

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}


manifest.xml

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

MyFirebaseMessagingService

public class MyFirebaseMessagingService extends FirebaseMessagingService {

    @Override
    public void onNewToken(@NonNull String s)
    {
        super.onNewToken(s);
        Log.d("My token", "Refreshed Token"+s);
        sendRegistrationToServer(s);
    }

    private void sendRegistrationToServer(String s) {
    }
    
}

If you ran the app on the device before adding the MyFirebaseMessagingService code, it probably already generated a token then. Adding the MyFirebaseMessagingService does not generate a new token, so your onNewToken will not be called.

The two options to get the token in that case are:

  1. Call FirebaseMessaging.getInstance().getToken() from (for example) you main activity and call the same sendRegistrationToServer there.
  2. Delete the app and its data after you added the FirebaseMessaging.getInstance().getToken() code, and reinstall the app. Deleting the data deletes the token, so then when you run the app it will generate a new token and call your onNewToken .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM