How to fixed issue "Default FirebaseApp failed to initialize because no default options were found"

Viewed 556

I tries to use Firebase Cloud Messaging for my app, and I coded follow guide "https://firebase.google.com/docs/cloud-messaging/android/client" to get device tooken

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

But when I run test on my device mobile, get issue in log:" D/FirebaseApp: Default FirebaseApp failed to initialize because no default options were found. This usually means that com.google.gms:google-services was not applied to your gradle project. E/AndroidRuntime: FATAL EXCEPTION: main Process: com.triaxlogistic.myapplication, PID: 32022 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.triaxlogistic.myapplication/com.triaxlogistic.myapplication.MainActivity}: java.lang.IllegalStateException: Default FirebaseApp is not initialized in this process com.triaxlogistic.myapplication. Make sure to call FirebaseApp.initializeApp(Context) first."

I don't know what I did wrong, even though I was add Firebase to my Android app in my project on the Firebase Console, and filled value the package name, value debug debugging certificate SHA1; download the latest json config file and copy to my project in Android studio. How to fix it?

0 Answers
Related