我正在制作一个聊天应用。在其中我添加了来自 firebase 的电话身份验证。当我点击登录时,它会在模拟器中询问 otp。我添加了我创建的测试号的 otp 它不会将我重定向到下一页。我检查了 Logcat,它说verificationId 为空。
这是我的第一页代码,用户输入他的电话号码-
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();
}
}
});
}
});
}
}
这里是活动图片-
[这是第一个活动]
[第二个活动]
答案 0 :(得分:0)
检查日志是否一切正常
并确保启用 SafetyNet https://console.cloud.google.com/apis/library/androidcheck.googleapis.com
当您完成所有必需的 SafetyNet 步骤后,它应该可以正常工作