Firebase 不发送 OTP

Firebase not sending OTP

我在 firebase 中使用 Firebase phone 号码 authentication 注册了一个用户。为了再次测试该功能,我从 [=12= 中删除了用户帐户] 安慰。现在,我正在尝试再次注册该号码,它直接转到 onVerificationCompleted() 回调而不发送 OTP。但是用户帐户未显示在 Firebase 控制台中。

请帮忙。

代码如下。另请注意,它向新号码发送 OTP。但不向我从 firebase 中删除的号码发送 OTP console.I 想再次注册该特定号码。

public class MainActivity extends AppCompatActivity {

private FirebaseAuth mAuth;

private boolean mVerificationInProgress = false;
private PhoneAuthProvider.OnVerificationStateChangedCallbacks mCallbacks;

private static final String KEY_VERIFY_IN_PROGRESS = "key_verify_in_progress";

private static final String TAG = "PhoneAuthActivity";
private String mVerificationId;
private PhoneAuthProvider.ForceResendingToken mResendToken;

private EditText mPhoneNumberField;

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

    if (savedInstanceState != null) {
        onRestoreInstanceState(savedInstanceState);
    }

    mPhoneNumberField=(EditText)findViewById(R.id.phone_number);

    mAuth = FirebaseAuth.getInstance();

    mCallbacks = new PhoneAuthProvider.OnVerificationStateChangedCallbacks() {

        @Override
        public void onVerificationCompleted(PhoneAuthCredential credential) {
            // This callback will be invoked in two situations:
            // 1 - Instant verification. In some cases the phone number can be instantly
            //     verified without needing to send or enter a verification code.
            // 2 - Auto-retrieval. On some devices Google Play services can automatically
            //     detect the incoming verification SMS and perform verification without
            //     user action.
            Log.d(TAG, "onVerificationCompleted:" + credential);
            Toast.makeText(getApplication(),"Verification completed",Toast.LENGTH_SHORT).show();
            // [START_EXCLUDE silent]
            mVerificationInProgress = false;

            Log.d(TAG,"CREDENTIAL"+credential);

//                Intent intent=new 
Intent(getBaseContext(),VerificationActivity.class);
//                intent.putExtra("VERIFICATION_ID",mVerificationId);
//                startActivity(intent);


        }




        @Override
        public void onVerificationFailed(FirebaseException e) {
            // This callback is invoked in an invalid request for verification is made,
            // for instance if the the phone number format is not valid.
            Log.w(TAG, "onVerificationFailed", e);
            // [START_EXCLUDE silent]
            mVerificationInProgress = false;
            // [END_EXCLUDE]

            if (e instanceof FirebaseAuthInvalidCredentialsException) {
                // Invalid request
                // [START_EXCLUDE]
                mPhoneNumberField.setError("Invalid phone number.");
                // [END_EXCLUDE]
            } else if (e instanceof FirebaseTooManyRequestsException) {
                // The SMS quota for the project has been exceeded
                // [START_EXCLUDE]
                Snackbar.make(findViewById(android.R.id.content), "Quota exceeded.",
                        Snackbar.LENGTH_SHORT).show();
                // [END_EXCLUDE]
            }


        }

        @Override
        public void onCodeSent(String verificationId,
                               PhoneAuthProvider.ForceResendingToken token) {
            // The SMS verification code has been sent to the provided phone number, we
            // now need to ask the user to enter the code and then construct a credential
            // by combining the code with a verification ID.
            Log.d(TAG, "onCodeSent:" + verificationId);

            // Save verification ID and resending token so we can use them later
            mVerificationId = verificationId;
            mResendToken = token;

            Intent intent=new Intent(getBaseContext(),VerificationActivity.class);
            intent.putExtra("VERIFICATION_ID",mVerificationId);
            startActivity(intent);


        }
    };
}


public void verifyDevice(View v){

    String phno=mPhoneNumberField.getText().toString();
    startPhoneNumberVerification(phno);


}

private void startPhoneNumberVerification(String phoneNumber) {
    // [START start_phone_auth]
    PhoneAuthProvider.getInstance().verifyPhoneNumber(
            phoneNumber,        // Phone number to verify
            60,                 // Timeout duration
            TimeUnit.SECONDS,   // Unit of timeout
            this,               // Activity (for callback binding)
            mCallbacks);        // OnVerificationStateChangedCallbacks
    // [END start_phone_auth]

    mVerificationInProgress = true;


}


@Override
public void onStart() {
    super.onStart();

    if (mVerificationInProgress) {
        startPhoneNumberVerification(mPhoneNumberField.getText().toString());
    }
    // [END_EXCLUDE]
}
// [END on_start_check_user]

@Override
protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    outState.putBoolean(KEY_VERIFY_IN_PROGRESS, mVerificationInProgress);
}

@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
    super.onRestoreInstanceState(savedInstanceState);
    mVerificationInProgress = savedInstanceState.getBoolean(KEY_VERIFY_IN_PROGRESS);
}

期待一些帮助。

尝试退出您的应用程序或简单地清除数据或重新安装,应用程序中的某处可能存在小故障。

如果上述指令不起作用,则原因有点深。由于身份验证是第一次起作用,因此很可能是网络运营商问题;这种情况主要发生在 phone 号码在某个时候从一个网络提供商转移到另一个网络提供商的情况。您可以尝试使用不同网络运营商的 phone 号码进行试验。 遗憾的是,目前还没有针对此问题的确切解决方法。无论哪种情况,您都可以将投诉发送至 Firebase support。那里肯定会有一些帮助。

根据我的经验,Firebase 在登录后创建一个刷新令牌和一个 firebase 用户 ID 令牌,firebase 用户 ID 令牌每小时过期一次,而刷新令牌是 long-lived。

此 firebase 用户 ID 令牌驻留在用户设备中,刷新令牌在 firebase 服务器上维护。

在您删除用户帐户后,刷新令牌必须已被 firebase 过期,但您设备上的用户 ID 令牌必须未过期,这就是它自动调用 onVerificationCompleted()

的原因

您可以阅读有关管理会话和令牌的信息here

为我工作。

有时 Google 无法验证来自您应用的请求。 为了解决它。生成应用程序的签名报告并获取 SHA-1 或 SHA-256 指纹。 转到您的 firebase 项目设置并更新这些指纹。

希望这个回答对您有所帮助。

我已将我的文件复制到另一台笔记本电脑上并尝试调试我的项目。我遇到了同样的问题,后来意识到我没有在 firebase 上更新我的新笔记本电脑的 SHA-1 和 SHA-256 指纹。

确保您的 SHA-1 和 SHA-256 指纹是最新的