简体   繁体   English

Firebase onTokenRefresh()从未调用

[英]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? 我需要做些初始化Firebase的事情吗?

Here is some background: 这里是一些背景:

I have a google-services.json file that I downloaded from the Firebase console stored under the src directory. 我有一个从Firebase控制台下载的google-services.json文件,存储在src目录下。

在此处输入图片说明

I have entered my debug.keystore SHA 1 fingerprint on the Firebase console. 我已经在Firebase控制台上输入了debug.keystore SHA 1指纹。

I have the following dependencies in my build.gradle file: 我的build.gradle文件中具有以下依赖build.gradle

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

I have followed the Firebase instructions to the letter: 我已按照Firebase的指示来信:

MyFirebaseInstanceIdService.java 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: 我将其放在清单的<application>标记内:

<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: 无奈之下,我将此行添加到MainActivity.java文件的onCreate()方法中:

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. java.lang.RuntimeException:无法启动活动ComponentInfo {com.mobilenicity.werun / com.mydomain.myapp.Activities.ViewControllers.MainActivity}:java.lang.IllegalStateException:名称为[DEFAULT]的FirebaseApp不存在。

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: 这是模块build.gradle文件:

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/'}
    }
}

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. 首先,我没有将类路径条目'com.google.gms:google-services:3.0.0'添加到我的根目录build.gradle文件中。

And additionally you have to add the following at the bottom of your module build.gradle file: 另外,您还必须在模块build.gradle文件的底部添加以下内容:

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 看一下Firebase设置说明: https : //firebase.google.com/docs/android/setup#add_firebase_to_your_app

Have you added the json config file upon setting up firebase? 设置Firebase时是否添加了json配置文件?

To add Firebase to your app you'll need a Firebase project and a Firebase configuration file for your app. 要将Firebase添加到您的应用程序,您需要一个Firebase项目和一个适用于您的应用程序的Firebase配置文件。

Create a Firebase project in the Firebase console, if you don't already have one. 如果您还没有一个Firebase项目,请在Firebase控制台中创建一个项目。 If you already have an existing Google project associated with your mobile app, click Import Google Project. 如果您已有与您的移动应用程序相关联的现有Google项目,请点击导入Google项目。 Otherwise, click Create New Project. 否则,单击创建新项目。

Click Add Firebase to your Android app and follow the setup steps. 点击将Firebase添加到您的Android应用,然后按照设置步骤进行操作。 If you're importing an existing Google project, this may happen automatically and you can just download the config file. 如果您要导入现有的Google项目,则该过程可能会自动发生,您只需下载配置文件即可。

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. 仅当您将应用程序添加到Firebase项目中时才能设置。

At the end, you'll download a google-services.json file. 最后,您将下载google-services.json文件。 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/. 如果尚未这样做,请将其复制到项目的模块文件夹中,通常为app /。

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: 我从Asynctask调用以下代码来获取Firebase令牌:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM