gpt4 book ai didi

Android Studio,Firebase 电话身份验证打开浏览器?

转载 作者:行者123 更新时间:2023-12-04 23:44:48 25 4
gpt4 key购买 nike

我正在尝试使用 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();
}
});
}

}

Gradle 文件

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'

}

最佳答案

您必须在您的谷歌云控制台中启用安全网才能按照此 Doc 使用电话身份验证

关于Android Studio,Firebase 电话身份验证打开浏览器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64785424/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com