Ad

Firebase SignIn Not Working On Lower Android Versions

So i have integrated Google and Facebook SignIn in my android app using firebase, and so far its working great on higher android versions like 9.0 (android P), 8.0 (android oreo) and 7.0 (android nogut) but are not working on lower android devices like android kitkat (4.4) , lollipop (5.0) etc.

I don't know what's wrong, also its not showing any logs,absolutely nothing. I have also released my app in the play store, but later came to know that its not working on some devices. Also i have already added the release SHA1 key on the firebase console.

Here is how i implemented the login,

 @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        // Pass the activity result back to the Facebook SDK
        mCallbackManager.onActivityResult(requestCode, resultCode, data);

        // Result returned from launching the Intent from GoogleSignInApi.getSignInIntent(...);
        if (requestCode == G_SIGN_IN) {
            Task<GoogleSignInAccount> task = GoogleSignIn.getSignedInAccountFromIntent(data);
            try {
                GoogleSignInAccount account = task.getResult(ApiException.class);
                firebaseAuthWithGoogle(Objects.requireNonNull(account));
            } catch (ApiException e) {
                Tools.createSnackBar(LoginActivity.this,"Google sign in failed! " + e.getLocalizedMessage(),Snackbar.LENGTH_SHORT).show();
                Log.d(TAG, e.getLocalizedMessage());
                googleBtn.setEnabled(true);
                pb.hide();
            }
        }
    }






 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);


        googleBtn = findViewById(R.id.google_btn);
        facebookBtn =   findViewById(R.id.facebook_btn);
        pb = findViewById(R.id.pb);
        pb.hide();

        mAuth = FirebaseAuth.getInstance();
        functions = new FirebaseFunctions(LoginActivity.this);


        //Google SignIn
        final GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
                .requestIdToken(getString(R.string.clientId))
                .requestEmail()
                .build();
        final GoogleSignInClient mGoogleSignInClient = GoogleSignIn.getClient(this, gso);

        googleBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                googleBtn.setEnabled(false);
                pb.show();
                Intent signInIntent = mGoogleSignInClient.getSignInIntent();
                startActivityForResult(signInIntent, G_SIGN_IN);
            }
        });



        //Facebook SignIn
        mCallbackManager = CallbackManager.Factory.create();
        facebookBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                final LoginManager loginManager = LoginManager.getInstance();
                loginManager.logInWithReadPermissions(LoginActivity.this, Arrays.asList(EMAIL, PROFILE));
                facebookBtn.setEnabled(false);

                loginManager.registerCallback(mCallbackManager, new FacebookCallback<LoginResult>() {
                    @Override
                    public void onSuccess(LoginResult loginResult) {
                        handleFacebookSignIn(loginResult.getAccessToken());
                    }

                    @Override
                    public void onCancel() {
                        Tools.createSnackBar(LoginActivity.this,"Login cancelled",Snackbar.LENGTH_SHORT).show();
                        facebookBtn.setClickable(true);
                    }

                    @Override
                    public void onError(FacebookException error) {
                        Tools.createSnackBar(LoginActivity.this,error.getMessage(),Snackbar.LENGTH_SHORT).show();
                        Log.d(TAG,error.getLocalizedMessage());
                        facebookBtn.setClickable(true);
                    }
                });
            }
        });

    }







  private void firebaseAuthWithGoogle(GoogleSignInAccount acct) {

        AuthCredential credential = GoogleAuthProvider.getCredential(acct.getIdToken(), null);
        mAuth.signInWithCredential(credential)
                .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
                    @Override
                    public void onComplete(@NonNull Task<AuthResult> task) {
                        if (task.isSuccessful()) {

                            FirebaseUser user = mAuth.getCurrentUser();
                            //Add new user
                            if (user != null) {
                                functions.addNewUser(user.getDisplayName(),user.getEmail(), Objects.requireNonNull(user.getPhotoUrl()).toString(), false, pb);
                            }

                        } else {
                            Tools.createSnackBar(LoginActivity.this,"Authentication Failed " + task.getException(),Snackbar.LENGTH_SHORT).show();
                            if(task.getException()!=null) {
                                Log.e(TAG, task.getException().getLocalizedMessage());
                            }
                            googleBtn.setEnabled(true);
                            pb.hide();
                        }

                    }
                });
    }





    private void handleFacebookSignIn(AccessToken accessToken) {

        pb.show();
        final AuthCredential credential = FacebookAuthProvider.getCredential(accessToken.getToken());
        mAuth.signInWithCredential(credential)
                .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
                    @Override
                    public void onComplete(@NonNull Task<AuthResult> task) {
                        if (task.isSuccessful()) {

                            FirebaseUser user = mAuth.getCurrentUser();
                            //Add new user
                            if (user != null) {
                                String email = user.getEmail();
                                String photoUrl = Objects.requireNonNull(user.getPhotoUrl()).toString();
                                photoUrl = photoUrl + "?height=500";
                                if(email==null){ email = "Email not found";}

                                functions.addNewUser(user.getDisplayName(),email, photoUrl, false, pb);
                            }

                        } else {
                            Tools.createSnackBar(LoginActivity.this,"Authentication failed "+task.getException(),Snackbar.LENGTH_SHORT).show();
                            if(task.getException()!=null) {
                                Log.e(TAG, task.getException().getLocalizedMessage());
                            }
                            facebookBtn.setEnabled(true);
                            pb.hide();
                        }
                    }
                });
    }
Ad

Answer

So i finally made it work, the issue was not related to firebase or authentication at all. Actually in the manifest my activity was set to singleInstance = "true" and noHistory = "true", So after i removed them, it worked fine.

Ad
source: stackoverflow.com
Ad