1

I'm using google's friendlychat android code from here: https://codelabs.developers.google.com/codelabs/firebase-android/#0

Code: https://github.com/firebase/friendlychat-android

Connected the chat app with firebase. Working fine in my android simulator (Pixel 2 XL API 27). Can sign in, send messages.

But whenever I use the app in my phone (Huawei Y5ii; old device), it shows up the sign in page. I click on sign in, select my email but it never opens up the chat screen. Can't figure out what's the problem.

SignInActivity.java

package com.google.firebase.codelab.friendlychat;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.google.android.gms.auth.api.Auth;
import com.google.android.gms.auth.api.signin.GoogleSignInAccount;
import com.google.android.gms.auth.api.signin.GoogleSignInOptions;
import com.google.android.gms.auth.api.signin.GoogleSignInResult;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.SignInButton;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.OnFailureListener;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.AuthCredential;
import com.google.firebase.auth.AuthResult;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.auth.GoogleAuthProvider;

public class SignInActivity extends AppCompatActivity implements GoogleApiClient.OnConnectionFailedListener,
        View.OnClickListener {

    private static final String TAG = "SignInActivity";
    private static final int RC_SIGN_IN = 9001;
    private SignInButton mSignInButton;

    private GoogleApiClient mGoogleApiClient;
    private FirebaseAuth mFirebaseAuth;

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

        // Assign fields
        mSignInButton = (SignInButton) findViewById(R.id.sign_in_button);

        // Set click listeners
        mSignInButton.setOnClickListener(this);

        GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
                .requestIdToken(getString(R.string.default_web_client_id))
                .requestEmail()
                .build();
        mGoogleApiClient = new GoogleApiClient.Builder(this)
                .enableAutoManage(this /* FragmentActivity */, this /* OnConnectionFailedListener */)
                .addApi(Auth.GOOGLE_SIGN_IN_API, gso)
                .build();

        // Initialize FirebaseAuth
        mFirebaseAuth = FirebaseAuth.getInstance();
    }

    private void handleFirebaseAuthResult(AuthResult authResult) {
        if (authResult != null) {
            // Welcome the user
            FirebaseUser user = authResult.getUser();
            Toast.makeText(this, "Welcome " + user.getEmail(), Toast.LENGTH_SHORT).show();

            // Go back to the main activity
            startActivity(new Intent(this, MainActivity.class));
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.sign_in_button:
                signIn();
                break;
            default:
                return;
        }
    }

    private void signIn() {
        Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient);
        startActivityForResult(signInIntent, RC_SIGN_IN);
    }

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

        // Result returned from launching the Intent from GoogleSignInApi.getSignInIntent(...);
        if (requestCode == RC_SIGN_IN) {
            GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
            if (result.isSuccess()) {
                // Google Sign In was successful, authenticate with Firebase
                GoogleSignInAccount account = result.getSignInAccount();
                firebaseAuthWithGoogle(account);
            } else {
                // Google Sign In failed
                Log.e(TAG, "Google Sign In failed.");
            }
        }
    }

    private void firebaseAuthWithGoogle(GoogleSignInAccount acct) {
        Log.d(TAG, "firebaseAuthWithGoogle:" + acct.getId());
        AuthCredential credential = GoogleAuthProvider.getCredential(acct.getIdToken(), null);
        mFirebaseAuth.signInWithCredential(credential)
                .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
                    @Override
                    public void onComplete(@NonNull Task<AuthResult> task) {
                        Log.d(TAG, "signInWithCredential:onComplete:" + task.isSuccessful());

                        // If sign in fails, display a message to the user. If sign in succeeds
                        // the auth state listener will be notified and logic to handle the
                        // signed in user can be handled in the listener.
                        if (!task.isSuccessful()) {
                            Log.w(TAG, "signInWithCredential", task.getException());
                            Toast.makeText(SignInActivity.this, "Authentication failed.",
                                    Toast.LENGTH_SHORT).show();
                        } else {
                            startActivity(new Intent(SignInActivity.this, MainActivity.class));
                            finish();
                        }
                    }
                });
    }

    @Override
    public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
        // An unresolvable error has occurred and Google APIs (including Sign-In) will not
        // be available.
        Log.d(TAG, "onConnectionFailed:" + connectionResult);
        Toast.makeText(this, "Google Play Services error.", Toast.LENGTH_SHORT).show();
    }

}
  • 1
    have you added your SHA1 of the release signing certificate to the firebase project and included the google-services.json in the project? – Arun Shankar Jul 27 '18 at 09:47
  • 1
    Please add logcat here :) – Quick learner Jul 27 '18 at 09:48
  • yes. done all that stuff. that's why the project is working fine in the android simulator. but not on my real device. –  Jul 27 '18 at 09:48
  • 1
    Check **[this](https://stackoverflow.com/questions/51360250/firebase-ui-authentication-with-google-fails-with-message-code10-message10)** out. Does it work? Please responde with @AlexMamo – Alex Mamo Jul 27 '18 at 09:49
  • 1
    Ok people. I'm going to paste the logcat here. And also checking your things out @AlexMamo –  Jul 27 '18 at 09:53
  • 1
    "have you added your SHA1 of the release signing certificate to the firebase project and included the google-services.json in the project?"- asked by Arun Shankar No I didn't! I just generated the SHA1 of the release signing certificate from your link Mr. AlexMamo and then added the SHA1 to the firebase project and download the latest google-services.json and replaced it in the app folder just like you said @ArunShankar And My Problem is now solved! Thanks guys! –  Jul 27 '18 at 11:08
  • 1
    @AbdullahAlNahid You're welcome, cheers! – Alex Mamo Jul 27 '18 at 11:15

3 Answers3

1

As we discussed in comments, I am adding this as an answer for any future reference:

The SHA1 signature of both debug and release signing certificate should be added to the firebase project and after that the google-services.json file from that firebase project have to be added in the app before deploying.

Arun Shankar
  • 2,603
  • 2
  • 26
  • 36
0

Thank you @Arun Shakkar for posting the correct answer. I just have to add that you can get your release key using the command. pathToYourKeyTol -list -keystore pathToYourKeyStore.jks eg "C:\Program Files\Java\jdk-version\bin\keytool" -list -keystore c:\Users\samund\path-to-prod-keystore.jks.

Though nothing is expected to change because of this, remember to download and use the updated version of google-services.json anytime you make changes to your app.

Samuel Nde
  • 2,565
  • 2
  • 23
  • 23
0
  1. Goto Play store and Search for "App Integrity"

Google play store app integrity

Then

  1. Copy the SHA Keys and Add into
Sayed Muhammad Idrees
  • 1,245
  • 15
  • 25