OnverificationCompleted() is called but not receiving any otp

試著忘記壹切 提交于 2019-12-13 04:08:05

问题


I'm using phone number verification on signing in with google or fb. The verification was working good with no issue for past 30 days and I didnt change the code at all. But suudenly, the otp is not sending to some mobile numbers sometimes not sending at all. My logcat has no error to my knowledge. But im facing this issue for last week. I need some help on this issue.


public class MainActivity extends FragmentActivity {

    private GoogleSignInClient mGoogleSignInClient;
    private String mVerificationId;
    private PinView pinView;


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

//Intialize the Frebase Auth
        mAuth = FirebaseAuth.getInstance();
        db = FirebaseFirestore.getInstance();

// Configure Google Sign In
        GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
                .requestIdToken(getString(R.string.default_web_client_id))
                .requestEmail().build();

// Initialize the SigninApi client for Google Signin
        mGoogleSignInClient = GoogleSignIn.getClient(this, gso);

 Button signInButton = findViewById(R.id.googlesignin);
        signInButton.setOnClickListener(v -> {
            signIn();
        });


}

//Google Signin Listener
    private void signIn() {
        Intent signInIntent = mGoogleSignInClient.getSignInIntent();
        startActivityForResult(signInIntent, 9001);
    }


    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 9001) {
            Task<GoogleSignInAccount> task = GoogleSignIn.getSignedInAccountFromIntent(data);
            try {
                // Google Sign In was successful, authenticate with Firebase
                GoogleSignInAccount account = task.getResult(ApiException.class);
                if (account != null) {
                    firebaseAuthWithGoogle(account);
                }
                Log.d("TAG", "Google sign in Success");
            } catch (ApiException e) {
                pbDialog.dismiss();
                // Google Sign In failed, update UI appropriately
                Log.d("TAG", "Google sign in failed", e);
                // ...
            }
        }
        else {
            mCallbackManager.onActivityResult(requestCode, resultCode, data);

        }
    }

    //Authentication check for google Sign in
    private void firebaseAuthWithGoogle(GoogleSignInAccount acct)
    {
        AuthCredential credential = GoogleAuthProvider.getCredential(acct.getIdToken(), null);
        mAuth.signInWithCredential(credential).addOnCompleteListener(this, task ->
        {
            if (task.isSuccessful())
            {// Sign in success, update UI with the signed-in user's information
                Log.d("TAG", "signInWithCredential:success");
                                Toast.makeText(this, FirebaseAuth.getInstance().getCurrentUser().getEmail(), Toast.LENGTH_SHORT).show();
                        //The above toast shows null email even after successfull sign
                boolean isNew = Objects.requireNonNull(task.getResult()).getAdditionalUserInfo().isNewUser();
                if(isNew) { pbDialog.dismiss(); showDialog();}
                else {checkgUserData();}
                //FirebaseUser user = mAuth.getCurrentUser();
            }
            else {// If sign in fails, display a message to the user.

                Log.w("TAG", "signInWithCredential:failure", task.getException());
                //Snackbar.make(findViewById(R.id.coo), "Authentication Failed.", Snackbar.LENGTH_SHORT).show();
            }
        });
    }

 private void showDialog(){
        Button sendotpbtn = findViewById(R.id.sendotp);
        EditText mnbox = findViewById(R.id.mn);
         Button confirmbtn = findViewById(R.id.confirmbtn);

        sendotpbtn.setOnClickListener(v -> {
            mobileno = mnbox.getText().toString();
            if(!mobileno.equals("")&&(mobileno.length()==10)){
                sendVerificationCode(mobileno);
            }
        });

confirmbtn.setOnClickListener(v -> {
            pinView = dialog.findViewById(R.id.firstPinView);
            String otp = Objects.requireNonNull(pinView.getText()).toString();
            if(!otp.equals("")) {
                verifyVerificationCode(pinView.getText().toString());

            }
            else {
                Toast.makeText(this, "OTP is incorrect", Toast.LENGTH_SHORT).show();
            }
        });
}


 private void sendVerificationCode(String mobile) {
        Toast.makeText(this, "Verification code sent", Toast.LENGTH_SHORT).show();
        PhoneAuthProvider.getInstance().verifyPhoneNumber(
                "+91" + mobile,
                60,
                TimeUnit.SECONDS,
                TaskExecutors.MAIN_THREAD,
                mCallbacks);
    }

    private PhoneAuthProvider.OnVerificationStateChangedCallbacks mCallbacks = new PhoneAuthProvider.OnVerificationStateChangedCallbacks() {
        @Override
        public void onVerificationCompleted(PhoneAuthCredential phoneAuthCredential) {
            //Getting the code sent by SMS
            String code = phoneAuthCredential.getSmsCode();
            if (code != null) {
                pinView.setText(code);
                verifyVerificationCode(code);
            }

        }

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

        @Override
        public void onCodeSent(String s, PhoneAuthProvider.ForceResendingToken forceResendingToken) {
           super.onCodeSent(s, forceResendingToken);
            mVerificationId = s;
        }
    };

    private void verifyVerificationCode(String otp) {
        Log.d("5","yes");
        //creating the credential
        PhoneAuthCredential credential = PhoneAuthProvider.getCredential(mVerificationId, otp);
        linkAccount(credential);
    }

    private void linkAccount(PhoneAuthCredential credential){
        try {
            Objects.requireNonNull(mAuth.getCurrentUser()).linkWithCredential(credential)
                    .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
                        @Override
                        public void onComplete(@NonNull Task<AuthResult> task) {
                            if (task.isSuccessful()) {
                                dialog.dismiss();
                                createUserData();
                                Log.d("TAG", "linkWithCredential:success");
                            }
                            if(!task.isSuccessful()&& task.getException() instanceof FirebaseAuthUserCollisionException){
                                Cancelbtn = dialog.findViewById(R.id.cancelbtn);
                                Cancelbtn.performClick();
                                Toast.makeText(MainActivity.this, "Mobile number is registered with another account", Toast.LENGTH_SHORT).show();

                            }
                            else {
                                Log.w("TAG", "linkWithCredential:failure", task.getException());

                            }
                        }
                    });
        }
        catch (NullPointerException e) {
            e.printStackTrace();
            Toast.makeText(this, "errorlll", Toast.LENGTH_SHORT).show();
        }
    }




}

Logcat

2019-09-13 13:53:27.724 12809-15029/? W/FirebaseAuth: [PhoneVerificationSession] PhoneVerificationSession constructor
2019-09-13 13:53:27.726 12809-2645/? W/FirebaseAuth: [PhoneNumberAuthPostProcessor] postProcess starts
2019-09-13 13:53:27.727 12809-15029/? W/FirebaseAuth: [PhoneVerificationSession] PhoneVerificationSession instant validation
2019-09-13 13:53:27.930 12809-2645/? W/FirebaseAuth: [PhoneNumberAuthPostProcessor] postProcess ends

来源:https://stackoverflow.com/questions/57919903/onverificationcompleted-is-called-but-not-receiving-any-otp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!