Firebase身份验证是否需要SMS Retriever API才能进行otp自动填充?

时间:2018-12-17 05:14:15

标签: android firebase firebase-authentication android-permissions

我正在使用Firebase auth api来验证用户电话号码。目前,在我的情况下,我有一个屏幕,用户在其中输入电话号码,在第二个屏幕上输入otp。根据文档firebase,大多数时间会自动检索otp并开始验证过程。 所以我的问题是它已经在Firebase Auth SDK中实现了此SMS Retriever API还是我应该自己实现以检索SMS并自动填充OTP。

1 个答案:

答案 0 :(得分:3)

不。我们不需要管理短信检索方案。

如果设备包含相同的SIM卡,则PhoneAuthProvider.OnVerificationStateChangedCallbacks将以onVerificationCompleted(PhoneAuthCredential phoneAuthCredential)方法自动对其进行管理。

代码段:

private PhoneAuthProvider.OnVerificationStateChangedCallbacks mCallbacks = new PhoneAuthProvider.OnVerificationStateChangedCallbacks() {

        @Override
        public void onVerificationCompleted(PhoneAuthCredential phoneAuthCredential) {
            Toast.makeText(FCMsmsTest.this, "onVerificationCompleted " + phoneAuthCredential.toString(), Toast.LENGTH_SHORT).show();
            signInWithPhoneAuthCredential(phoneAuthCredential);
        }

        @Override
        public void onVerificationFailed(FirebaseException e) {
            Toast.makeText(FCMsmsTest.this, "onVerificationFailed " + e.toString(), Toast.LENGTH_SHORT).show();

            if (e instanceof FirebaseAuthInvalidCredentialsException) {
                Toast.makeText(FCMsmsTest.this, "Invalid Request " + e.toString(), Toast.LENGTH_SHORT).show();
            } else if (e instanceof FirebaseTooManyRequestsException) {
                Toast.makeText(FCMsmsTest.this, "The SMS quota for the project has been exceeded " + e.toString(), Toast.LENGTH_SHORT).show();
            }
        }

        @Override
        public void onCodeSent(String verificationId,
                               PhoneAuthProvider.ForceResendingToken token) {
            Toast.makeText(FCMsmsTest.this, "onCodeSent " + verificationId, Toast.LENGTH_SHORT).show();
            editText.setText("");

            mVerificationId = verificationId;
            PhoneAuthProvider.ForceResendingToken mResendToken = token;

            showDialog();
        }
    };