首页 > 解决方案 > 如何使用 java android 在 firebase 中发送 otp 代码短信?

问题描述

我有一个带有 firebase 的 Android Studio 项目,我已经正确实现了 firebase。

我正在尝试使用短信身份验证登录,一切正常,但没有短信到达在此处输入图像描述

我已经配置了 firebase 身份验证

这是一些代码

mCallbacks = new PhoneAuthProvider.OnVerificationStateChangedCallbacks() {
                @Override
                public void onVerificationCompleted(PhoneAuthCredential credential) {
                    Toast.makeText(MainActivity.this, "Verification Complete", Toast.LENGTH_SHORT).show();
                }

                @Override
                public void onVerificationFailed(FirebaseException e) {
                    Toast.makeText(MainActivity.this, "Verification Failed", Toast.LENGTH_SHORT).show();
                }

                @Override
                public void onCodeSent(String verificationId,
                                       PhoneAuthProvider.ForceResendingToken token) {
                    Toast.makeText(MainActivity.this, "Code Sent", Toast.LENGTH_SHORT).show();
                }
            };
@Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.bt_send_otp:

                PhoneAuthProvider.getInstance().verifyPhoneNumber(
                        etPhone.getText().toString(),        // Phone number to verify
                        1,                 // Timeout duration
                        TimeUnit.MINUTES,   // Unit of timeout
                        this,               // Activity (for callback binding)
                        mCallbacks);        // OnVerificationStateChangedCallbacks

                break;
            case R.id.bt_resend_otp:
                break;
            case R.id.bt_verify_otp:
                break;
        }
   }

当我按下发送 otp 按钮时,吐司“代码已发送”出现

谢谢您的帮助 :)

标签: javaandroidfirebase

解决方案


既然是测试号,OTP就不会到了。虽然对于测试,您可以使用您添加的验证码和控制台上的测试电话号码。 Firebase Console Dashboard-> Authentication-> SignIn Method-> Phone-> Phone numbers for testing

在此处输入图像描述

查看以下文档Firebase Phone VerificationAndroidhttps: //firebase.google.com/docs/auth/android/phone-auth? authuser=1#test-with-whitelisted-phone-numbers


推荐阅读