如何在Java Android中使用Firebase发送OTP验证码短信?

huangapple go评论165阅读模式
英文:

How to send otp code sms in firebase with java android?

问题

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

我正在尝试使用短信验证进行登录,一切都正常,但是没有收到短信如何在Java Android中使用Firebase发送OTP验证码短信?

我已经配置了 Firebase 验证。

以下是一些代码示例:

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

    @Override
    public void onVerificationFailed(FirebaseException e) {
        Toast.makeText(MainActivity.this, "验证失败", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onCodeSent(String verificationId,
                           PhoneAuthProvider.ForceResendingToken token) {
        Toast.makeText(MainActivity.this, "验证码已发送", 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(),        // 要验证的手机号码
                    1,                 // 超时时间
                    TimeUnit.MINUTES,   // 超时时间单位
                    this,               // 活动(用于回调绑定)
                    mCallbacks);        // OnVerificationStateChangedCallbacks
            break;
        case R.id.bt_resend_otp:
            break;
        case R.id.bt_verify_otp:
            break;
    }
}

当我点击“发送验证码”按钮时,会显示 Toast 提示“验证码已发送”。

感谢您的帮助 如何在Java Android中使用Firebase发送OTP验证码短信?

英文:

I have a Android Studio project with firebase, I already implement firebase correctly.

im trying to make a sign in with sms authentication, everything works but no sms arrives如何在Java Android中使用Firebase发送OTP验证码短信?

I already configure firebase authentication

here's some code

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;
        }
   }

when I press the send otp button the toast "Code Sent" appears

Thanks for the help 如何在Java Android中使用Firebase发送OTP验证码短信?

答案1

得分: 0

由于这是一个测试号码,OTP 将不会到达。但是为了测试,您可以在控制台上使用您添加到测试电话号码的验证代码。
Firebase 控制台仪表盘 -> 身份验证 -> 登录方法 -> 电话 -> 用于测试的电话号码

查看 Firebase 手机验证 的文档,适用于 Android:https://firebase.google.com/docs/auth/android/phone-auth?authuser=1#test-with-whitelisted-phone-numbers

英文:

Since it is a test number, OTP will not arrive. Though for testing you can use the verification code you added with the test phone number on the console.
Firebase Console Dashboard -> Authentication -> SignIn Method -> Phone -> Phone numbers for testing.

如何在Java Android中使用Firebase发送OTP验证码短信?

Check out the docs for Firebase Phone Verification for Android : https://firebase.google.com/docs/auth/android/phone-auth?authuser=1#test-with-whitelisted-phone-numbers

huangapple
  • 本文由 发表于 2020年4月9日 02:10:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/61107237.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定