首页 > 解决方案 > otp 自动填充的 Firebase 身份验证是否需要 SMS Retriever API?

问题描述

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

标签: androidfirebasefirebase-authenticationandroid-permissions

解决方案


没有。我们不需要管理短信检索场景。

如果设备包含相同的 SIM 卡,则由PhoneAuthProvider.OnVerificationStateChangedCallbacksinonVerificationCompleted(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();
        }
    };

推荐阅读