调用OnverificationCompleted()但未收到任何otp

时间:2019-09-13 08:26:09

标签: android firebase google-cloud-firestore firebase-authentication

我在通过google或fb登录时使用电话号码验证。过去30天内,验证工作正常,没有任何问题,我完全没有更改代码。但令人惊讶的是,otp并没有发送到某些手机号码,有时甚至根本没有发送。据我所知,我的logcat没有错误。但是我上个星期面对这个问题。在这个问题上我需要一些帮助。


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

0 个答案:

没有答案