Search code examples
firebasefirebase-authenticationfirebase-cloud-messagingandroid-12android-13

Firebase Cloud Messaging fails authentication for SDK 31+ on Android 12+


I set up FCM for my app. And it works perfectly for devices run under Android 11 and less. But for devices under Android 12 and greater I can't receive token by using

FirebaseMessaging.getInstance().token.addOnCompleteListener {
            ...
        } 

I receive an error: java.io.IOException: java.util.concurrent.ExecutionException: java.io.IOException: AUTHENTICATION_FAILED instead of token

Manifest

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    package="com.boltic28.learnmultiplying">

    <uses-permission android:name="android.permission.POST_NOTIFICATIONS"/>

    <application
        android:allowBackup="true"
        android:dataExtractionRules="@xml/data_extraction_rules"
        android:fullBackupContent="@xml/backup_rules"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.LearnMultiplying"
        tools:targetApi="31">
        <activity
            android:name=".MainActivity"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <service
            android:name=".services.firebase.AppFirebaseMessagingService"
            android:exported="false">

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

        <meta-data
            android:name="com.google.firebase.messaging.default_notification_channel_id"
            android:value="@string/default_notification_channel_id" />

    </application>

</manifest>

app:

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

android {
    compileSdk 33

    defaultConfig {
        applicationId "com.boltic28.learnmultiplying"
        minSdk 26
        targetSdk 33
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    ...
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_11
        targetCompatibility JavaVersion.VERSION_11
    }
    kotlinOptions {
        jvmTarget = '11'
    }
    ...
}

dependencies {
    ...
    implementation 'com.google.firebase:firebase-messaging:23.0.4'
    ...
}

service:

class AppFirebaseMessagingService : FirebaseMessagingService() {

    override fun onNewToken(token: String) {
        super.onNewToken(token)

        println("->> new token: $token")
    }

    override fun onMessageReceived(message: RemoteMessage) {
        super.onMessageReceived(message)

        println("->> new message: ${message.notification?.title} - ${message.notification?.body}")
    }
}

Does anybody have some suggestions?

Data: App targeted: SDK33 Run under : SDK31 FCM version : 23.0.6


Solution

  • The issue was with an emulator, FCM doesn't provide a token for devices under SDK31+ without Google play services. To solve the issue and get a token you have to use an emulator or device with installed Google play services, Google APIs are not enough. Emulators with Google play

    How to create Emulator with Google Play