FirebaseInstanceId 不再存在

我刚刚升级了我的所有Firebase依赖项,但我遇到了一个问题,无法再识别。我用它来获取令牌ID,如下所示:FirebaseInstanceId

String tokenId = FirebaseInstanceId.getInstance().getToken();

这是错误:

Cannot resolve symbol 'FirebaseInstanceId'

这是我的build.gradle文件:

//Firebase
implementation 'com.google.firebase:firebase-auth:16.0.1'
implementation 'com.google.firebase:firebase-database:16.0.1'
implementation 'com.google.firebase:firebase-firestore:17.0.1'

//FirebaseUI
implementation 'com.firebaseui:firebase-ui-auth:4.0.0'
implementation 'com.firebaseui:firebase-ui-database:4.0.0'
implementation 'com.firebaseui:firebase-ui-firestore:4.0.0'

如何使用最新的依赖项获取令牌 ID?


答案 1

我也遇到了同样的问题。从文档中,它说FirebaseInstanceId不再可用,并说使用FirebaseMessaging.getInstance().token,如下所示

FirebaseMessaging.getInstance().token.addOnCompleteListener(OnCompleteListener { task ->
    if (!task.isSuccessful) {
        Log.w(TAG, "Fetching FCM registration token failed", task.exception)
        return@OnCompleteListener
    }

    // Get new FCM registration token
    val token = task.result
})

答案 2

将以下内容添加到 build.gradle 文件中:

 implementation 'com.google.firebase:firebase-messaging:17.0.0'
 implementation 'com.google.firebase:firebase-core:16.0.0'

更多信息在这里:

https://firebase.google.com/docs/cloud-messaging/android/client#set-up-firebase-and-the-fcm-sdk


推荐