我正在尝试使用Firebase在我的应用程序中添加电话身份验证,当我单击“发送OTP”按钮时,浏览器将打开并显示验证您不是机器人,并且仅在通过选择图像验证我不是机器人之后,它正在发送OTP。
OTP的格式也已更改。
以前的OTP格式为
435325是您对MyApplication的验证码。
OTP的新格式为
789574,如果您的验证代码为myapplication.55b19.firebaseapp.com
我只是不明白为什么每次我请求OTP时浏览器都会打开
这是我的代码
public class MainActivity extends AppCompatActivity {
EditText phoneEt,otpEt;
Button loginBtn,verifyOTPBtn;
private String verificationCodeBySystem;
private FirebaseAuth firebaseAuth;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
firebaseAuth = FirebaseAuth.getInstance();
phoneEt = findViewById(R.id.phoneEt);
otpEt = findViewById(R.id.otpEt);
loginBtn = findViewById(R.id.loginBtn);
verifyOTPBtn = findViewById(R.id.verifyOTPBtn);
loginBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String phoneNumber = phoneEt.getText().toString().trim();
sendOTPCodeToUser(phoneNumber);
}
});
verifyOTPBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String code = otpEt.getText().toString().trim();
if (code.isEmpty() || code.length() < 6){
otpEt.setError("Wrong OTP...");
otpEt.requestFocus();
return;
}
verifyCode(code);
}
});
}
private void sendOTPCodeToUser(String phoneNumber) {
PhoneAuthOptions options =
PhoneAuthOptions.newBuilder(firebaseAuth)
.setPhoneNumber("+91"+phoneNumber) // Phone number to verify
.setTimeout(60L, TimeUnit.SECONDS) // Timeout and unit
.setActivity(this) // Activity (for callback binding)
.setCallbacks(mCallbacks) // OnVerificationStateChangedCallbacks
.build();
PhoneAuthProvider.verifyPhoneNumber(options);
}
private PhoneAuthProvider.OnVerificationStateChangedCallbacks mCallbacks = new PhoneAuthProvider.OnVerificationStateChangedCallbacks() {
@Override
public void onCodeSent(@NonNull String s, @NonNull PhoneAuthProvider.ForceResendingToken forceResendingToken) {
super.onCodeSent(s, forceResendingToken);
verificationCodeBySystem = s;
}
@Override
public void onVerificationCompleted(@NonNull PhoneAuthCredential phoneAuthCredential) {
String code = phoneAuthCredential.getSmsCode();
verifyCode(code);
}
@Override
public void onVerificationFailed(@NonNull FirebaseException e) {
}
};
private void verifyCode(String code) {
try{
PhoneAuthCredential credential = PhoneAuthProvider.getCredential(verificationCodeBySystem,code);
signInTheCredentials(credential);
}catch (Exception e){
}
}
private void signInTheCredentials(PhoneAuthCredential credential) {
FirebaseAuth firebaseAuth = FirebaseAuth.getInstance();
firebaseAuth.signInWithCredential(credential)
.addOnCompleteListener(task -> {
if (task.isSuccessful()){
Toast.makeText(MainActivity.this, "Success", Toast.LENGTH_SHORT).show();
}else {
Toast.makeText(MainActivity.this, "Wrong OTP", Toast.LENGTH_SHORT).show();
}
});
}
}
成绩文件
plugins {
id 'com.android.application'
id 'com.google.gms.google-services'
}
android {
compileSdkVersion 30
buildToolsVersion "30.0.2"
defaultConfig {
applicationId "package.myapplication"
minSdkVersion 24
targetSdkVersion 30
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}
dependencies {
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'com.google.android.material:material:1.2.1'
implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
implementation 'com.google.firebase:firebase-auth:20.0.0'
testImplementation 'junit:junit:4.+'
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
implementation platform('com.google.firebase:firebase-bom:26.0.0')
implementation 'android.arch.persistence.room:runtime:1.1.1'
implementation "androidx.browser:browser:1.2.0"
annotationProcessor 'android.arch.persistence.room:compiler:1.1.1'
}