Search code examples
androidfirebasefirebase-authenticationbackground-service

Firebase Auth in Background Service


I have an android background service, on onCreate() function I run this

FirebaseAuth mAuth = FirebaseAuth.getInstance();

mAuth.signInWithEmailAndPassword("user@user.com", "password")
    .addOnCompleteListener((Executor)this, new OnCompleteListener<AuthResult>() {
        @Override
        public void onComplete(@NonNull Task<AuthResult> task) {
            if (task.isSuccessful()) {
                Log.d(Actions.LOG_TAG, "signInWithEmail:success");
                FirebaseUser user = mAuth.getCurrentUser();

            } else {
                Log.w(Actions.LOG_TAG, "signInWithEmail:failure", task.getException());
            }
        }
    });

but it crashes. Why? catch exception are

network provider does not exist, provider doesn't exist: network

Attempt to invoke virtual method 'com.google.android.gms.tasks.Task com.google.android.gms.common.api.GoogleApi.zzb(com.google.android.gms.common.api.internal.zzde)' on a null object reference java.lang.NullPointerException: Attempt to invoke virtual method 'com.google.android.gms.tasks.Task com.google.android.gms.common.api.GoogleApi.zzb(com.google.android.gms.common.api.internal.zzde)' on a null object reference

EDIT

Same problem putting code in onStart() function in MainActivity (following this guide https://firebase.google.com/docs/auth/android/password-auth)


Solution

  • Emulator does not have Google Play Services installed.

    Firebase Auth does not support devices without play services.

    If you want to use Firebase you need to use Firebase Admin SDK.