0
votes

I have included SHA1, and SHA-256 in firebase project.

while fragment is running an Exception Toast Message appears: "This request is missing a valid app indenitier, meaning that neither SafetyNet checks nor reCAPTCHA checks succeeded. Please try again, or checkc the logcat for more details".

Code:

public class VerifyPhoneFragment extends Fragment {

    public static String source = null;
    PinView pinView;
    String phoneNo;
    boolean verified = false;
    private String VerificationCodSent;
    private String CodeEnteredByUser;


    public VerifyPhoneFragment() {
        // Required empty public constructor
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState){

        final NavController navController = Navigation.findNavController(getActivity(), R.id.nav_host_fragment);
        final ProgressBar progressBar = view.findViewById(R.id.progress_bar);
        ImageView backArrowImageView = view.findViewById(R.id.back_arrow);
        TextView backTextView = view.findViewById(R.id.back_text_view);
        TextView otpMobileTextView = view.findViewById(R.id.otp_mobile_tv);
        Button verifyButton = view.findViewById(R.id.verify_button);
        pinView = view.findViewById(R.id.pin_view);

        
        if(source.equals("SignUpFragment")){
            phoneNo = "+" + SignUpFragment.mobileTxt;

        }
        else if(source.equals("LoginFragment")){
            phoneNo = "+" + LoginFragment.mobileTxt;
        }
        else if(source.equals("ForgotPasswordFragment")){
            phoneNo = "+" + ForgotPasswordFragment.mobileTxt;
        }
        otpMobileTextView.setText("Enter OTP sent to Your Phone" + "\n" + phoneNo);

        backArrowImageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                navController.navigate(R.id.verifyPhone_to_login);
            }
        });
        backTextView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                navController.navigate(R.id.verifyPhone_to_login);
            }
        });

        PhoneAuthProvider.getInstance().verifyPhoneNumber(
                phoneNo,
                60,
                TimeUnit.SECONDS,
                getActivity(),
                new PhoneAuthProvider.OnVerificationStateChangedCallbacks(){
                    @Override
                    public void onVerificationCompleted(@NonNull PhoneAuthCredential phoneAuthCredential) {
                        VerificationCodSent = phoneAuthCredential.getSmsCode();
                        if(VerificationCodSent != null){
                            progressBar.setVisibility(View.VISIBLE);
                            verifyButton.setVisibility(view.INVISIBLE);

                            PhoneAuthCredential phoneAuthCredential1 = PhoneAuthProvider.getCredential(
                                    VerificationCodSent,
                                    CodeEnteredByUser
                            );
                            FirebaseAuth.getInstance().signInWithCredential(phoneAuthCredential).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
                                @Override
                                public void onComplete(@NonNull Task<AuthResult> task) {
                                    progressBar.setVisibility(View.GONE);
                                    verifyButton.setVisibility(View.VISIBLE);
                                    if(task.isSuccessful()){
                                        verified = true;
                                    }
                                    else{
                                        Toast.makeText(getContext(), "the Verification Code entered was invalid", Toast.LENGTH_LONG).show();
                                        verified = false;
                                    }
                                }
                            });
                        }
                    }

                    @Override
                    public void onVerificationFailed(@NonNull FirebaseException e) {
                        Toast.makeText(getContext(), e.getMessage(), Toast.LENGTH_LONG).show();
                    }

                    @Override
                    public void onCodeSent(@NonNull String VerificationCodeSent, @NonNull PhoneAuthProvider.ForceResendingToken forceResendingToken) {
                        VerificationCodSent = VerificationCodeSent;
                    }
                }
        );

        verifyButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(pinView.getText().toString().isEmpty()){
                    Toast.makeText(getContext(), "Please enter a valid verification code", Toast.LENGTH_LONG).show();
                    verified = false;
                    goToAppropriateFragment(source);
                }

                CodeEnteredByUser = pinView.getText().toString();

                if(VerificationCodSent != null){
                    progressBar.setVisibility(View.VISIBLE);
                    verifyButton.setVisibility(view.INVISIBLE);

                    PhoneAuthCredential phoneAuthCredential = PhoneAuthProvider.getCredential(
                            VerificationCodSent,
                            CodeEnteredByUser
                    );
                    FirebaseAuth.getInstance().signInWithCredential(phoneAuthCredential).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
                        @Override
                        public void onComplete(@NonNull Task<AuthResult> task) {
                            progressBar.setVisibility(View.GONE);
                            verifyButton.setVisibility(View.VISIBLE);
                            if(task.isSuccessful()){
                                verified = true;
                            }
                            else{
                                Toast.makeText(getContext(), "the Verification Code entered was invalid", Toast.LENGTH_LONG).show();
                                verified = false;
                            }
                        }
                    });
                }

                // afterVerification
                goToAppropriateFragment(source);
            }
        });

    }

    public void goToAppropriateFragment(String source){

        final NavController navController = Navigation.findNavController(getActivity(), R.id.nav_host_fragment);

        if(source.equals("SignUpFragment")){
            if(verified){
                DatabaseHelper databaseHelper = new DatabaseHelper(getContext());

                if(databaseHelper.addNewUser()){ // if the user added successfully addUser() returns true
                    Toast.makeText(getContext(), "You have signed up successfully! you can login", Toast.LENGTH_LONG).show();
                    // go to LoginFragment
                    navController.navigate(R.id.verifyPhone_to_login);
                }
                else{ // to be deleted won't  arrive here if email is already used
                    Toast.makeText(getContext(), "Sorry, Unable to sign you up! Try again later", Toast.LENGTH_LONG).show();
                }
            }
            else {
                // Toast.makeText(getContext(), "Sorry, it seems that you have not entered the OTP correctly. Try again", Toast.LENGTH_LONG).show();
                // go to SignUpFragment
                navController.navigate(R.id.verifyPhone_to_signUp);
            }
        }
        else if(source.equals("LoginFragment")){
            if(verified){
                // go to MainContentActivity
                Toast.makeText(getContext(), "Welcome back " + LoginFragment.fNameTxt + "!", Toast.LENGTH_LONG).show();
                Intent intent = new Intent(getContext(), MainContentActivity.class);
                startActivity(intent);
            }
            else {
                // Toast.makeText(getContext(), "Sorry, it seems that you have not entered the OTP correctly. Try again", Toast.LENGTH_LONG).show();
                // go to LoginFragment
                navController.navigate(R.id.verifyPhone_to_login);
            }
        }
        else if(source.equals("ForgotPasswordFragment")){
            if(verified){
                // go to ResetPasswordFragment
                navController.navigate(R.id.verifyPhone_to_resetPassword);
            }
            else {
                Toast.makeText(getContext(), "Sorry, it seems that you have not entered the OTP correctly. Try again", Toast.LENGTH_LONG).show();
                // go to LoginFragment
                navController.navigate(R.id.verifyPhone_to_login);
            }
        }
    }
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_verify_phone, container, false);
    }
}

Logcat:

2021-03-30 20:57:21.769 16847-16847/com.example.map_project_v2 E/SpannableStringBuilder: SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length 2021-03-30 20:57:50.049 16847-16847/com.example.map_project_v2 E/zzf: Problem retrieving SafetyNet Token: 7: 2021-03-30 20:57:51.267 16847-17116/com.example.map_project_v2 E/FirebaseAuth: [GetAuthDomainTask] Error getting project config. Failed with INVALID_CERT_HASH 400 2021-03-30 20:57:51.381 16847-16847/com.example.map_project_v2 E/zzf: Failed to get reCAPTCHA token with error [There was an error while trying to get your package certificate hash.]- calling backend without app verification 2021-03-30 20:57:51.763 16847-16976/com.example.map_project_v2 E/FirebaseAuth: [SmsRetrieverHelper] SMS verification code request failed: unknown status code: 17093 null

1
Which line is this exception thrown on? - Boris Strandjev
Is it possible you have 16847 lines in your file? What stands on this line? - Boris Strandjev

1 Answers

0
votes

I was getting the same error and I could solved it with the last two steps of the following (make sure you have covered all of them):

  1. Enable Phone option in Sign-in method under Firebase Authentication
  2. Make sure to download and add the latest google-services.json file in your project
  3. Enable Android Device Verification for your firebase project in https://console.cloud.google.com/
  4. Add library implementation "androidx.browser:browser:1.3.0" https://developer.android.com/jetpack...