You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

升级Firebase依赖后FirebaseInstanceId无法识别,如何获取Token ID?

How to Get FCM Token with Updated Firebase Dependencies (Replacing FirebaseInstanceId)

Hey there! It looks like you're hitting a super common issue after upgrading Firebase — FirebaseInstanceId has been deprecated and removed in newer Firebase versions, which is exactly why your old code is throwing that "Cannot resolve symbol" error. Let's fix this step by step.

Step 1: Update Your Firebase Dependencies

First, let's get your dependencies up to date (using Firebase's Bill of Materials (BOM) will automatically avoid version conflicts). Replace your existing Firebase-related lines in your module-level build.gradle with this:

// Import Firebase BOM to manage all Firebase SDK versions
implementation platform('com.google.firebase:firebase-bom:32.7.0')

// Add Firebase SDKs without version numbers (BOM handles versioning)
implementation 'com.google.firebase:firebase-auth'
implementation 'com.google.firebase:firebase-database'
implementation 'com.google.firebase:firebase-firestore'
implementation 'com.google.firebase:firebase-messaging' // Required for FCM token retrieval

// Update FirebaseUI to a version compatible with the BOM
implementation 'com.firebaseui:firebase-ui-auth:8.0.2'
implementation 'com.firebaseui:firebase-ui-database:8.0.2'
implementation 'com.firebaseui:firebase-ui-firestore:8.0.2'

Step 2: Replace the Old Token Retrieval Code

FirebaseInstanceId.getInstance().getToken() is no longer valid. Instead, use FirebaseMessaging to fetch the token asynchronously (this is the recommended approach to avoid blocking the main thread):

// Fetch FCM token asynchronously
FirebaseMessaging.getInstance().getToken()
    .addOnCompleteListener(task -> {
        if (!task.isSuccessful()) {
            Log.w("FCM_TOKEN", "Failed to fetch token", task.getException());
            return;
        }

        // Get the retrieved token
        String fcmToken = task.getResult();
        // Use the token here (e.g., log it, send to your backend server)
        Log.d("FCM_TOKEN", "Successfully retrieved token: " + fcmToken);
    });

FCM tokens can refresh automatically (like when the app is reinstalled or the user clears app data). To capture these updates, create a custom FirebaseMessagingService:

public class MyFirebaseMessagingService extends FirebaseMessagingService {
    @Override
    public void onNewToken(String token) {
        super.onNewToken(token);
        // This method triggers whenever a new token is generated
        Log.d("FCM_TOKEN", "Refreshed token: " + token);
        // Send this updated token to your backend server here
    }
}

Then register this service in your AndroidManifest.xml:

<application ...>
    <!-- Other app components -->

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

Why This Works

Firebase replaced FirebaseInstanceId with FirebaseMessaging for all token-related operations in newer SDK versions. Using the BOM ensures all your Firebase libraries are fully compatible, and the asynchronous method is more performant than the old synchronous call.

内容的提问来源于stack exchange,提问作者Always Learner

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.29 07:52:55