【问题标题】:"FCM" App Crashes on this line getToken() "java.lang.IllegalStateException: Default FirebaseApp is not initialized in this process" [duplicate]“FCM”应用程序在此行崩溃getToken()“java.lang.IllegalStateException:默认FirebaseApp未在此过程中初始化”[重复]
【发布时间】:2019-08-18 00:34:24
【问题描述】:

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

我开始研究 FCM 模块,我按照 Firebase 指南进行了休闲。我遇到了两个异常,例如 RuntimeException 和 IllegalStateException。 Firebase 初始化默认值,但 FCM 中出现问题的原因和原因。

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

2。我闲暇了设置 Firebase 云消息传递。

我在下面链接:

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

应用程序: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 Token了。

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

按照以下解决方案交叉检查项目:build.gradle 文件和应用程序: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
}

应用程序: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'

【问题讨论】:

  • 确保您在应用中添加了 google json 文件,并在文件中使用您的包名检查包名。最后添加 FirebaseApp.initializeApp(this);活动中
  • 是的,我添加了 google json 文件。
  • Firebase 需要时间来连接到它的服务器并为您提供引用您设备的实例,给它一些时间或使用承诺来确保 Firebase 已连接到它的服务器并生成实例,然后执行需要 firebase 实例的操作。
  • 我终于找到了解决方案。只需在项目 build.gradle 文件中添加单行代码,即 mavenLocal();

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


【解决方案1】:

试试这个方法。


在 Android Studio 中转到工具菜单。
然后选择 Firebase 选项。
提供您启用了 Firebase 的 google 帐户 id-password。
从右侧选项卡中选择云消息传递。
然后点击 setup firebase messages 。
按照那边的mension步骤。

【讨论】:

    【解决方案2】:

    我可以从您的代码中看到,您在必须初始化 Firebase 实例之前就获得了它。您必须先初始化它,然后使用getInstance() 获取令牌。

    FirebaseApp.initializeApp(this);
    

    或者尝试谷歌文档显示获取令牌的方式

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

    【讨论】:

    • 检查更新的答案
    • 是的,我检查了上面的答案仍然得到相同的异常和相同的崩溃。
    猜你喜欢
    • 1970-01-01
    • 2021-01-30
    • 2017-10-25
    • 1970-01-01
    • 2019-07-05
    • 2020-08-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多