0

I'm running into an issue where I'm trying to getToken() from Firebase, but I'm unable to get the token because of the following error:

Attempt to invoke virtual method 'com.google.android.gms.tasks.Task com.google.firebase.iid.FirebaseInstanceId.getInstanceId()' on a null object reference
FlameDra
  • 1,807
  • 7
  • 30
  • 47
  • Is your service registered in the manifest? – cutiko Feb 07 '19 at 19:27
  • @cutiko added my manifest file in main post. – FlameDra Feb 07 '19 at 19:37
  • You only need MyFirebaseMessagingService https://firebase.google.com/docs/cloud-messaging/android/client?hl=es-419 – cutiko Feb 07 '19 at 20:03
  • Seems like your Firebase Messaging is not getting initialized. Have you done anything to prevent auto initialization as mentioned here. https://firebase.google.com/docs/cloud-messaging/android/client – Ranjan Feb 08 '19 at 04:07

4 Answers4

1

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 already using onNewToken as you can see in my code. I thought FirebaseInstanceId.getInstance().getInstanceId() is the new non-deprecated method in activitis? Why would I get null for FirebaseInstanceId.getInstance() when I try to access it in my Notification.java file? – FlameDra Feb 07 '19 at 22:33
  • No getInstanceId() is deprecaed. See here https://stackoverflow.com/questions/51125169/what-to-use-now-that-firebaseinstanceid-getinstance-gettoken-is-deprecated – Abhinav Suthar Feb 08 '19 at 03:24
  • It says "This method was deprecated. In favour of getInstanceId()" which is what I am using. – FlameDra Feb 08 '19 at 19:39
0

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

FirebaseApp.initializeApp(Your activity);

R.Devi
  • 1
  • 2
0

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();

Irfan Yaqub
  • 140
  • 10
0

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

Karringgton
  • 207
  • 1
  • 3
  • 11