“ FCM”应用程序在此行崩溃getToken()“ java.lang.IllegalStateException:在此过程中未初始化默认FirebaseApp”

时间:2019-03-27 12:31:00

标签: java android firebase android-studio android-developer-api

FCM(Firebase云消息传递)初始化问题

我开始研究FCM模块,但按照Firebase指南休假。我收到两个类似RuntimeException和IllegalStateException的异常。 Firebase初始默认设置,但是为什么以及FCM中的问题是什么。

1。通过Android Studio工具-> Firebase->云消息传递。

2。我不喜欢设置Firebase Cloud Mesasging。

我喜欢下面的链接:

https://firebase.google.com/docs/cloud-messaging/android/client

App:Build.gradle

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    //noinspection GradleCompatible
    implementation 'com.android.support:appcompat-v7:28.0.0'
    implementation 'com.android.support.constraint:constraint-layout:1.1.3'
    implementation 'com.google.firebase:firebase-messaging:17.4.0'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}

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

项目:Build.gradle

buildscript {
    repositories {
        google()
        jcenter()

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

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        google()
        jcenter()

    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

在MainActivity.class中,我尝试获取令牌,但应用程序崩溃。

 public class MainActivity extends AppCompatActivity {

    public static final String TAG = MainActivity.class.getSimpleName();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        String tokenId = FirebaseInstanceId.getInstance().getToken(); //Crashes in this line 
        Log.d("SH", tokenId);
    }
}

在logcat中,我有两个例外

 E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.package PID: 29368
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.package.testfcm/com.package.MainActivity}: java.lang.IllegalStateException: Default FirebaseApp is not initialized in this process com.package. Make sure to call FirebaseApp.initializeApp(Context) first.
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2861)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2943)
        at android.app.ActivityThread.-wrap11(Unknown Source:0)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1630)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:164)
        at android.app.ActivityThread.main(ActivityThread.java:6626)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:811)
     Caused by: java.lang.IllegalStateException: Default FirebaseApp is not initialized in this process com.package. Make sure to call FirebaseApp.initializeApp(Context) first.
        at com.google.firebase.FirebaseApp.getInstance(com.google.firebase:firebase-common@@16.0.2:240)
        at com.google.firebase.iid.FirebaseInstanceId.getInstance(Unknown Source:1)
        at com.package.MainActivity.onCreate(MainActivity.java:20)
        at android.app.Activity.performCreate(Activity.java:7032)
        at android.app.Activity.performCreate(Activity.java:7023)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1236)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2814)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2943) 
        at android.app.ActivityThread.-wrap11(Unknown Source:0) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1630) 
        at android.os.Handler.dispatchMessage(Handler.java:106) 
        at android.os.Looper.loop(Looper.java:164) 
        at android.app.ActivityThread.main(ActivityThread.java:6626) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:811) 

最后,我找到了解决方案,现在我可以获取FCM令牌。

在项目build.gradle文件中,您需要添加单行代码,即mavenLocal()。

请按照以下解决方案中的一个进行交叉检查项目:build.gradle文件和app:build.gradle文件。

项目:build.gradle

buildscript {
    repositories {
        mavenLocal()  //This is the line you need to add 
        google()
        jcenter()

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

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        mavenLocal() //This is the line you need to add 
        google()
        jcenter()

    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

App:build.gradle

apply plugin: 'com.android.application'


android {
    compileSdkVersion 28
    defaultConfig {
        applicationId "com.example.testfcm"
        minSdkVersion 21
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    //noinspection GradleCompatible
    implementation 'com.android.support:appcompat-v7:28.0.0'
    implementation 'com.android.support.constraint:constraint-layout:1.1.3'


    implementation 'com.google.firebase:firebase-core:16.0.8'
    implementation 'com.google.firebase:firebase-iid:17.1.1'
    implementation 'com.google.firebase:firebase-messaging:17.5.0'

    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}
apply plugin: 'com.google.gms.google-services'

2 个答案:

答案 0 :(得分:0)

我从您的代码中看到,您必须先初始化Firebase实例。您必须先对其进行初始化,然后使用getInstance()获取令牌。

FirebaseApp.initializeApp(this);

或者尝试使用Google文档显示的方式来获取令牌

FirebaseInstanceId.getInstance().getInstanceId()
        .addOnCompleteListener(new OnCompleteListener<InstanceIdResult>() {
            @Override
            public void onComplete(@NonNull Task<InstanceIdResult> task) {
                if (!task.isSuccessful()) {
                    Log.w(TAG, "getInstanceId failed", task.getException());
                    return;
                }

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


                Toast.makeText(MainActivity.this, token, Toast.LENGTH_SHORT).show();
            }
        });

答案 1 :(得分:0)

尝试这种方式。


在Android Studio中,转到“工具”菜单。
然后选择Firebase选项。
提供您启用了Firebase的Google帐户ID密码。
在右侧选项卡中,选择云消息传递。
然后单击设置Firebase消息传递。
按照那里的提到步骤。