[英]Firebase phone authentication not working in android
我正在制作一个聊天应用程序。 在其中我添加了来自 firebase 的电话身份验证。 当我点击登录时,它会在模拟器中询问 otp。 我添加了我创建的测试编号的 otp,它不会将我重定向到下一页。 我检查了 Logcat,它说 verifyId 是 null。
这是我的第一页代码,用户输入他的电话号码 -
package com.example.nowchat;
导入androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.renderscript.ScriptGroup;
import android.view.View;
import com.example.nowchat.databinding.ActivityLoginBinding;
public class login extends AppCompatActivity {
ActivityLoginBinding binding;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = ActivityLoginBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());
getSupportActionBar().hide();
binding.phoneBox.requestFocus();
binding.getotp.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
Intent intent = new Intent(login.this, OTP.class);
intent.putExtra("phone", binding.phoneBox.getText().toString());
startActivity(intent);
}
});
}
}
这是用户输入 otp 且未重定向到第三页的页面代码-
package com.example.nowchat;
导入androidx.annotation.NonNull; 导入androidx.appcompat.app.AppCompatActivity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;
import com.example.nowchat.databinding.ActivityOTPBinding;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.FirebaseException;
import com.google.firebase.auth.AuthResult;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.PhoneAuthCredential;
import com.google.firebase.auth.PhoneAuthOptions;
import com.google.firebase.auth.PhoneAuthProvider;
import com.mukesh.OnOtpCompletionListener;
import java.util.concurrent.TimeUnit;
public class OTP extends AppCompatActivity {
ActivityOTPBinding binding;
FirebaseAuth auth;
String verificationId;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = ActivityOTPBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());
auth = FirebaseAuth.getInstance();
getSupportActionBar().hide();
String phoneNumber = getIntent().getStringExtra("phone");
binding.phoneLbl.setText("Verify " + phoneNumber);
PhoneAuthOptions options = PhoneAuthOptions.newBuilder(auth)
.setPhoneNumber(phoneNumber)
.setTimeout(60L, TimeUnit.SECONDS)
.setActivity(OTP.this)
.setCallbacks(new PhoneAuthProvider.OnVerificationStateChangedCallbacks() {
@Override
public void onVerificationCompleted(@NonNull PhoneAuthCredential phoneAuthCredential)
{
}
@Override
public void onVerificationFailed(@NonNull FirebaseException e) {
}
@Override
public void onCodeSent(@NonNull String verifyId, @NonNull
PhoneAuthProvider.ForceResendingToken forceResendingToken) {
super.onCodeSent(verifyId, forceResendingToken);
verificationId = verifyId;
//verificationId ="and";
}
}).build();
PhoneAuthProvider.verifyPhoneNumber(options);
binding.otpView.setOtpCompletionListener(new OnOtpCompletionListener() {
@Override
public void onOtpCompleted(String otp) {
Log.i("abcd", "verification id is"+ verificationId);
Log.i("abcd", "otp id is" + otp);
PhoneAuthCredential credential = PhoneAuthProvider.getCredential(verificationId, otp);
auth.signInWithCredential(credential).addOnCompleteListener(new
OnCompleteListener<AuthResult>(){
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
Intent intent = new Intent(OTP.this, SetupProfileActivity.class);
startActivity(intent);
finishAffinity();
} else {
Toast.makeText(OTP.this, "Failed", Toast.LENGTH_SHORT).show();
}
}
});
}
});
}
}
以下是活动图片——
[第一次活动]
【第二次活动】
检查日志是否一切正常
并确保启用 SafetyNet https://console.cloud.google.com/apis/library/androidcheck.googleapis.com
完成所有必需的 SafetyNet 步骤后,它应该可以正常工作
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.