AndroidDev
AndroidDev

Reputation: 21237

Firebase onTokenRefresh() never called

I've read other posts on this issue, but none of them are helping me. My issue is that I NEVER get a token. I can reinstall the app, whatever, it never generates.

Is there something that I need to do to initialize Firebase?

Here is some background:

I have a google-services.json file that I downloaded from the Firebase console stored under the src directory.

enter image description here

I have entered my debug.keystore SHA 1 fingerprint on the Firebase console.

I have the following dependencies in my build.gradle file:

compile 'com.google.firebase:firebase-core:9.4.0'
compile 'com.google.firebase:firebase-messaging:9.4.0'

I have followed the Firebase instructions to the letter:

MyFirebaseInstanceIdService.java

public class MyFirebaseInstanceIdService extends FirebaseInstanceIdService {

    private static final String TAG = "MyFirebaseIIDService";    

    @Override
    public void onTokenRefresh() {
        Log.d(TAG, "on token refresh");
        // Get updated InstanceID token.
        String token = FirebaseInstanceId.getInstance().getToken();
        Log.d(TAG, "Refreshed token: " + token);

        sendRegistrationToServer(token);
    }

    private void sendRegistrationToServer(String token) {
    }
}

And I have this within the <application> tag in the manifest:

<service
android:name=".HelperClasses.PushNotifications.MyFirebaseInstanceIdService">
    <intent-filter>
        <action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
    </intent-filter>
</service>

In desperation, I added this line to the onCreate() method of my MainActivity.java file:

FirebaseInstanceId.getInstance().getToken();

This just crashes the app with this error:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.mobilenicity.werun/com.mydomain.myapp.Activities.ViewControllers.MainActivity}: java.lang.IllegalStateException: FirebaseApp with name [DEFAULT] doesn't exist.

Of course, I've researched that error, but none of the solutions have helped me.

No idea where to turn next, but I'm clearly missing something. Can anyone shed some light on this? Thank you.

EDIT

Here is the module build.gradle file:

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:2.1.3'
        classpath 'com.neenbedankt.gradle.plugins:android-apt:1.4'
        classpath 'com.google.gms:google-services:3.0.0'
    }
}

allprojects {
    repositories {
        jcenter()
        mavenCentral()
        maven {url 'https://oss.sonatype.org/content/repositories/snapshots/'}
    }
}

Upvotes: 0

Views: 2535

Answers (3)

Shubham Maheshwari
Shubham Maheshwari

Reputation: 139

I am not sure of your Implementation, but I do have another approach that I have used which works.

I call the following code from Asynctask to Fetch Firebase Token:

FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
String userId = user.getUid();
Task<GetTokenResult> z1 = user.getToken(false);//true means it force refreshes the token, false means will update only if expired-- token by default expires in an hour
        try {
            Tasks.await(z1);
        } catch (Exception e) {
            e.printStackTrace();
        }
final String z2 = z1.getResult().getToken();

Upvotes: 0

Axel
Axel

Reputation: 21

I had the same issue.

Firstly I had missed to add the classpath entry 'com.google.gms:google-services:3.0.0' to my root level build.gradle file.

And additionally you have to add the following at the bottom of your module build.gradle file:

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

Have a look at the firebase setup instructions: https://firebase.google.com/docs/android/setup#add_firebase_to_your_app

Upvotes: 2

Jaythaking
Jaythaking

Reputation: 2102

Have you added the json config file upon setting up firebase?

To add Firebase to your app you'll need a Firebase project and a Firebase configuration file for your app.

Create a Firebase project in the Firebase console, if you don't already have one. If you already have an existing Google project associated with your mobile app, click Import Google Project. Otherwise, click Create New Project.

Click Add Firebase to your Android app and follow the setup steps. If you're importing an existing Google project, this may happen automatically and you can just download the config file.

When prompted, enter your app's package name. It's important to enter the package name your app is using; this can only be set when you add an app to your Firebase project.

At the end, you'll download a google-services.json file. You can download this file again at any time. If you haven't done so already, copy this into your project's module folder, typically app/.

Upvotes: 0

Related Questions