java - 如何使用 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 按钮时,吐司“代码已发送”出现
谢谢您的帮助 :)
解决方案
既然是测试号,OTP
就不会到了。虽然对于测试,您可以使用您添加的验证码和控制台上的测试电话号码。
Firebase Console Dashboard
-> Authentication
-> SignIn Method
-> Phone
-> Phone numbers for testing
。
查看以下文档Firebase Phone Verification
:Android
https: //firebase.google.com/docs/auth/android/phone-auth? authuser=1#test-with-whitelisted-phone-numbers
推荐阅读
- python - 从现有 Python 程序中为 Python 程序打开单独的 CMD
- mysql - 如何在同一个表中建立多个关系ManyToMany
- c++ - .dylib 外部符号在 Mac OSX 上发生冲突,如何从应用程序已加载的外部符号中重新加载外部符号的私有副本?
- c# - "找不到方法:'System.Text.Json.JsonEncodedText System.Text.Json.JsonEncodedText.Encode(System.String,System.Text.Encodings.Web.JavaScriptEncoder)
- reactjs - 如果我尝试在 webpack 模式下运行:为什么我无法正确获取 Material-ui 样式:React.js 中的“生产”?
- android - 为 textColor edittext 禁用暗模式
- java - Java中带计数器的一系列字符串操作操作
- android - 加载位图图像时回收器视图滚动滞后
- python - 如何打印对象内容而不是它们的内存位置?
- python - 将列表值分配给参数