Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FirebaseInstanceId.getInstance() returns null

I am integrating Firebase Cloud messaging in android

app build.gradle looks like this

implementation 'com.google.firebase:firebase-auth:16.0.3'
implementation 'com.google.firebase:firebase-messaging:17.3.3'
implementation 'com.google.android.gms:play-services-auth:16.0.0'


apply plugin: 'com.google.gms.google-services'

class path in apllication level build.gradle is

dependencies {
        classpath 'com.android.tools.build:gradle:3.2.0'
        classpath 'com.google.gms:google-services:4.0.1'
    }

and my manifest is something like that

<service android:name=".firebase.MyFirebaseMessagingService">
       <intent-filter>
          <action android:name="com.google.firebase.MESSAGING_EVENT" />
       </intent-filter>
  </service>
       <meta-data
        android:name="com.google.firebase.messaging.default_notification_icon"
        android:resource="@drawable/ic_artize_a" />

    <meta-data
        android:name="com.google.firebase.messaging.default_notification_color"
        android:resource="@color/colorAccent" />

    <meta-data
        android:name="com.google.firebase.messaging.default_notification_channel_id"
        android:value="@string/default_notification_channel_id" />

My problem is that When I try to get FirebaseInstanceId.getInstance() it returns null. so I am unable to get Token.

Even onNewToken of MyFirebaseMessagingService is not fired a single time in app.

Can Some one tell me why i am getting FirebaseInstanceId.getInstance() null here thanks in advance.

Here i am trying to get firebase token

private void getFirebaseToken(){
        FirebaseInstanceId firebaseInstanceId = FirebaseInstanceId.getInstance();
        if(firebaseInstanceId == null){
            return;
        }


        firebaseInstanceId.getInstanceId().addOnCompleteListener(new OnCompleteListener<InstanceIdResult>() {
            @Override
            public void onComplete(@NonNull Task<InstanceIdResult> task) {
                if (!task.isSuccessful()) {
                    return;
                }

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

            }
        });
    }
like image 972
Navin Gupta Avatar asked Oct 03 '18 13:10

Navin Gupta


2 Answers

After Long research, I found that tools:node="replace" in the manifest file is making FCM to not work.

Change tools:node="replace" to tools:node="merge"

go through this link

https://github.com/firebase/quickstart-android/issues/477

like image 141
creativecoder Avatar answered Sep 19 '22 23:09

creativecoder


try to replace in your Android Manifest application tag with the below attributes

android:allowBackup="false" tools:node="merge" tools:replace="android:label,android:allowBackup"

like image 22
Vinoth M Avatar answered Sep 17 '22 23:09

Vinoth M