alphaguy
alphaguy

Reputation: 540

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

I am to using firebase-messaging library and trying to fetch the token using below method on app launch.

FirebaseInstanceId.getInstance().getInstanceId().addOnSuccessListener(new OnSuccessListener() {
           @Override
           public void onSuccess(InstanceIdResult instanceIdResult) {
               String token = instanceIdResult.getToken();
               // print token
           }
       });

App crashes on the launch itself giving

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

AndroidManifest

<service
      android:name=".MyFirebaseMessageService">
      <intent-filter>
            <action android:name="com.google.firebase.MESSAGING_EVENT"/>
      </intent-filter>
</service>

Build.gradle :

implementation 'com.google.android.gms:play-services-analytics:16.0.5'
implementation 'com.google.firebase:firebase-messaging:17.3.4'

Also, i have tried to cache the token in SharedPreferences but it seems onNewToken() is never called.

@Override
    public void onNewToken(String token) {
        super.onNewToken(token);
        SharedPreferences preference = PreferenceManager.getDefaultSharedPreferences(this);
        SharedPreferences.Editor editor = preference.edit();
        editor.putString("TOKEN",token);
        editor.apply();
    }

What could be the problem?

Upvotes: 8

Views: 5126

Answers (3)

Abdul Gani
Abdul Gani

Reputation: 689

You need add the below packages,

  1. Xamarin.Firebase.Lid
  2. Xamarin.Firebase.Messaging
  3. Xamarin.Firebase.Common

Optional - Xamarin.Firebase.Analytics (For analytics)

Upvotes: 0

shams keshk
shams keshk

Reputation: 33

I Face the same problem while merging .aar manifest with the main app manifest , As @alphaguy mention that there are missing Components after merging the manifest , And After add the following service as he mention the error is gone :

<service
        android:name="com.google.firebase.components.ComponentDiscoveryService"
        android:exported="false" >
        <meta-data
            android:name="com.google.firebase.components:com.google.firebase.iid.Registrar"
            android:value="com.google.firebase.components.ComponentRegistrar" />
        <meta-data
            android:name="com.google.firebase.components:com.google.firebase.messaging.FirebaseMessagingRegistrar"
            android:value="com.google.firebase.components.ComponentRegistrar" />
    </service>

But still firebase Messaging not work because WakefulBroadcastReceiver that receives FirebaseInstanceId and FirebaseMessaging events and delivers them to the application-specific FirebaseInstanceIdService is missing too , This receiver is automatically added to your application's manifest file via manifest merge , in our case it was missing in the merge so we need to add it manually as follow :

<receiver
        android:name="com.google.firebase.iid.FirebaseInstanceIdReceiver"
        android:exported="false"
        android:permission="com.google.android.c2dm.permission.SEND" >
        <intent-filter>
            <action android:name="com.google.android.c2dm.intent.RECEIVE" />
        </intent-filter>
    </receiver>

FirbaseInstanceIdReceiver Documentation : https://firebase.google.com/docs/reference/android/com/google/firebase/iid/FirebaseInstanceIdReceiver

  • Note : that you may need to add FirebaseApp.initializeApp(this); at onCreate in your ApplicationClass .

    public class MyApplicationClass extends Application {
    
    @Override
    public void onCreate() {
        super.onCreate();
        FirebaseApp.initializeApp(this);
    }
    

Upvotes: 0

alphaguy
alphaguy

Reputation: 540

There was problem in merged manifest, following service was missing from the merged manifest. Added same to AndroidManifest.xml it worked like a charm.

 <service android:name="com.google.firebase.components.ComponentDiscoveryService" >
            <meta-data
                android:name="com.google.firebase.components:com.google.firebase.iid.Registrar"
                android:value="com.google.firebase.components.ComponentRegistrar" />
 </service>

Everything is working fine now.

Upvotes: 14

Related Questions