简体   繁体   中英

FirebaseInstanceId.getInstance() is always null

我遇到了一个问题,我试图从 Firebase 获取 getToken(),但由于以下错误,我无法获取令牌:

Attempt to invoke virtual method 'com.google.android.gms.tasks.Task com.google.firebase.iid.FirebaseInstanceId.getInstanceId()' on a null object reference

FirebaseInstanceId.getInstance() is deprecated
You can get firebase_token in onNewToken() function

public class MyFirebaseMessagingService extends FirebaseMessagingService {

@Override
public void onNewToken(String s) {
    super.onNewToken(s);
    // s is your token
    // Do whatever you want with it
    // You can store it in SharedPreferences
}}

I'm not sure but You have to initialize Firebase in your activity

FirebaseApp.initializeApp(Your activity);

please check if you added this (com.google.firebase:firebase-messaging:17.3.4')dependency in your gradle file. for frther help you can get the help from here . and if you want to get the Token this way. String refreshedToken = FirebaseInstanceId.getInstance().getToken();

I'm running into the same problem when I build the application with BUCK but I am able to use Firebase correctly when I build with Gradle. You may want to verify that Firebase was initialized. You should see this in your logs if so:

I/FirebaseInitProvider: FirebaseApp initialization successful

Looking at the documentation here: https://firebase.google.com/docs/reference/android/com/google/firebase/iid/FirebaseInstanceId , FirebaseInstanceId.getInstance().getToken() is deprecated in favor of getInstanceId().

Instead, this documentation shows how to get the current firebase token: https://firebase.google.com/docs/cloud-messaging/android/client?authuser=1#retrieve-the-current-registration-token

FirebaseInstanceId.getInstance().getInstanceId()
        .addOnCompleteListener(new OnCompleteListener<InstanceIdResult>() {
            @Override
            public void onComplete(@NonNull Task<InstanceIdResult> task) {
                if (!task.isSuccessful()) {
                    Log.w(TAG, "getInstanceId failed", task.getException());
                    return;
                }

                // Get new Instance ID token
                String token = task.getResult().getToken();

                // Log and toast
                String msg = getString(R.string.msg_token_fmt, token);
                Log.d(TAG, msg);
                Toast.makeText(MainActivity.this, msg, Toast.LENGTH_SHORT).show();
            }
        });

This documentation says that the onNewToken method gets called when the token has been updated: https://firebase.google.com/docs/cloud-messaging/android/client?authuser=1#monitor-token-generation

/**
 * Called if InstanceID token is updated. This may occur if the security of
 * the previous token had been compromised. Note that this is called when the InstanceID token
 * is initially generated so this is where you would retrieve the token.
 */
@Override
public void onNewToken(String token) {
    Log.d(TAG, "Refreshed token: " + token);

    // If you want to send messages to this application instance or
    // manage this apps subscriptions on the server side, send the
    // Instance ID token to your app server.
    sendRegistrationToServer(token);
}

With Gradle, I was able to get this to work with these libraries:

 implementation 'com.google.firebase:firebase-messaging:18.0.0' implementation 'com.google.firebase:firebase-auth:17.0.0'

I had migrated from GCM to FCM, and the documentation was straight forward for getting it to work with Gradle: https://developers.google.com/cloud-messaging/android/android-migrate-fcm

This documentation seems straight forward as well if you are using Gradle to build: https://firebase.google.com/docs/cloud-messaging/android/client?authuser=1#monitor-token-generation

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