6

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

            }
        });
    }
13
  • 1
    Show where in your code you're calling FirebaseInstanceId.getInstance() Commented Oct 3, 2018 at 13:16
  • May be If you are calling FirebaseInstanceId.getInstance() on UIThread it is giving you null. Try to execute it on separate thread. Commented Oct 3, 2018 at 13:19
  • I am calling FirebaseInstanceId.getInstance() in OnCreate method of an activity Commented Oct 3, 2018 at 13:21
  • 1
    @m0skit0 I have tries two ways.First time i called this method in AsyncTask's doInBackground it doesn't work then i make a thread and call this method it also not worked............... Commented Oct 4, 2018 at 5:09
  • 1
    @NavinGupta I am having the same issue , did you find any solution ? Commented Dec 26, 2018 at 9:15

4 Answers 4

7

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

Sign up to request clarification or add additional context in comments.

Comments

3

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"

2 Comments

Thank you Vinoth! Do you know what causing this?
@JerabekJakub when you build the apk file, gradle not include content provider ( FirebaseInitProvider class) in android manifest file
0

Having FirebaseInstanceId.getInstance(); on the UI thread shouldn't be a problem. I have the following in the onCreate method of the MainActivity and it works perfectly (probably not recommended by google and others):

FirebaseInstanceId.getInstance().getInstanceId().addOnCompleteListener(instance_id_completion_listener);

Try the following steps:

  1. Add implementation 'com.google.firebase:firebase-core:16.0.1' to the app build.gradle file.

  2. Remove, but back it up, all manifest entries related to Firebase. I don't have any in my manifest file however I am only using Firebase to get an instance id.

  3. If one hasn't done so already go to https://firebase.google.com/docs/android/setup and add Firebase to Your Android Project.

One could also try the following:

private void getFirebaseToken(){

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

            Log.d("getFirebaseToken", "Successful");

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

        }
    });
}

For testing purposes only as this is not a good idea on the UI thread, try the following:

private void getFirebaseToken(){
    FirebaseInstanceId firebaseInstanceId = FirebaseInstanceId.getInstance();

    try
    {
        Thread.sleep(2000);
    } 
    catch (InterruptedException e)
    {
        e.printStackTrace();
    }

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

        }
    });
}

Comments

0

For me there was a missing declaration in the AndroidManifest.xml

<application ... >

    <meta-data
        android:name="com.google.android.gms.version"
        android:value="@integer/google_play_services_version" />

After adding it, the issue was resolved.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.