FirebaseInstanceId不再存在

时间:2018-05-29 08:26:46

标签: android firebase android-studio gradle firebase-realtime-database

我刚刚对所有Firebase依赖项进行了升级,但我遇到了问题,FirebaseInstanceId无法识别。我用它来获取这样的令牌ID:

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?

4 个答案:

答案 0 :(得分:3)

我也遇到了同样的问题。实际上,FirebaseInstanceId 已被关闭,取而代之的是 Firebase 安装。

试试:

FirebaseInstallations.getToken();

答案 1 :(得分:2)

添加到 Gradle:

  implementation 'com.google.firebase:firebase-messaging:22.0.0'

  implementation 'com.google.firebase:firebase-core:19.0.0'

然后添加以下代码以获取令牌:

public static String returnMeFCMtoken() {
        final String[] token = {""};
        FirebaseMessaging.getInstance().getToken().addOnCompleteListener(new OnCompleteListener<String>() {
            @Override
            public void onComplete(@NonNull Task<String> task) {
                if(task.isComplete()){
                    token[0] = task.getResult();
                    Log.e("AppConstants", "onComplete: new Token got: "+token[0] );

                }
            }
        });
        return token[0];
    }

答案 2 :(得分:2)

我也遇到了同样的问题。从 doc,它说 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
})

答案 3 :(得分:1)

将以下内容添加到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