如何解决问题“默认FirebaseApp初始化失败,因为未找到默认选项”

时间:2019-06-07 12:05:25

标签: firebase token

我尝试将Firebase Cloud Messaging用于我的应用程序,并编写了遵循指南“ https://firebase.google.com/docs/cloud-messaging/android/client”的代码,以获取设备

MainActivity:

FirebaseApp.initializeApp(this);

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

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

                        // Log and toast
                        String msg = getString(R.string.msg_token_fmt, token);
                        Log.d("FCM", msg);
                        Toast.makeText(MainActivity.this, msg, Toast.LENGTH_SHORT).show();
                    }
                });

app.gradle:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 28
    defaultConfig {
        applicationId "com.triaxlogistic.myapplication"
        minSdkVersion 16
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
        multiDexEnabled true
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        useLibrary 'org.apache.http.legacy'

    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    packagingOptions {
        exclude 'META-INF/DEPENDENCIES'
        exclude 'META-INF/LICENSE'
        exclude 'META-INF/LICENSE.txt'
        exclude 'META-INF/license.txt'
        exclude 'META-INF/NOTICE'
        exclude 'META-INF/NOTICE.txt'
        exclude 'META-INF/notice.txt'
        exclude 'META-INF/ASL2.0'
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:support-vector-drawable:28.0.0'
    implementation 'com.android.support:support-v4:28.0.0'
    androidTestImplementation('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    implementation 'com.android.support:multidex:1.0.3'
    implementation 'com.android.support:design:28.0.0'
    implementation 'com.android.support.constraint:constraint-layout:1.1.3'
    implementation 'com.google.android.libraries.places:places:1.0.0'
    implementation 'com.facebook.android:account-kit-sdk:5.0.0'
    implementation 'com.google.firebase:firebase-core:16.0.6'
    implementation 'com.google.firebase:firebase-messaging:18.0.0'
    implementation 'com.android.volley:volley:1.1.0'
    implementation 'net.gotev:uploadservice:2.1'
    testImplementation 'junit:junit:4.12'
core:3.0.2'

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

AndroidManifest

        <service android:name=".utilities.MyFirebaseMessagingService"
            android:exported="false">
            <intent-filter>
                <action android:name="com.google.firebase.MESSAGING_EVENT" />
            </intent-filter>
        </service>

MyFirebaseMessagingService

public class MyFirebaseMessagingService extends FirebaseMessagingService {

    private static final String TAG = "MyFirebaseMsgService";

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        Log.d(TAG, "From: " + remoteMessage.getFrom());
        //TODO: xu ly thong bao co du lieu
        if (remoteMessage.getData().size() > 0) {
            sendNotification(remoteMessage.getData());
        }
        //TODO: xu ly thong bao tin thong thuong
        if (remoteMessage.getNotification() != null) {
            sendNotificationBody(remoteMessage.getNotification().getBody());
        }
    }

    @Override
    public void onNewToken(String token) {
        Log.d(TAG, "Refreshed token: " + token);

        sendRegistrationToServer(token);
    }
....

但是当我在移动设备上运行测试时,在日志中出现问题:” D / FirebaseApp:默认FirebaseApp无法初始化,因为未找到默认选项。这通常意味着com.google.gms:google-services未应用于您的gradle项目。 E / AndroidRuntime:致命异常:main     进程:com.triaxlogistic.myapplication,PID:32022     java.lang.RuntimeException:无法启动活动ComponentInfo {com.triaxlogistic.myapplication / com.triaxlogistic.myapplication.MainActivity}:java.lang.IllegalStateException:默认FirebaseApp在此进程com.triaxlogistic.myapplication中未初始化。确保先调用FirebaseApp.initializeApp(Context)。“

我不知道自己做错了什么 在Firebase控制台上的项目中,将Firebase添加到我的Android应用中,并填入程序包名称,调试调试证书SHA1;下载最新的json配置文件并复制到我在Android Studio中的项目。 如何解决?

0 个答案:

没有答案