Villez请帮我检查一下吗?我想使用Android Studio Firebase(云Firestore)进行Otp身份验证。该过程似乎正确,但是输入号码后我没有收到代码。我的意思是输入电话号码并单击验证按钮后,应该自动向该号码发送代码,但是我什么都没收到。请帮助我知道出了什么问题。这是我的代码
更新:抱歉,延迟。我最终找到了它。唯一有问题的是我在登录方法中使用了测试编号。现在代码可以正常使用
Register.java
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.OnFailureListener;
import com.google.android.gms.tasks.OnSuccessListener;
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.PhoneAuthProvider;
import com.google.firebase.firestore.DocumentReference;
import com.google.firebase.firestore.DocumentSnapshot;
import com.google.firebase.firestore.FirebaseFirestore;
import com.rilixtech.widget.countrycodepicker.CountryCodePicker;
import java.util.concurrent.TimeUnit;
public class Register2 extends AppCompatActivity {
FirebaseAuth fAuth;
String phoneNumber;
String otpCode ;
String verificationId;
EditText phone,optEnter;
Button next;
CountryCodePicker countryCodePicker;
PhoneAuthCredential credential;
Boolean verificationOnProgress = false;
ProgressBar progressBar;
TextView state,resend;
PhoneAuthProvider.ForceResendingToken token;
FirebaseFirestore fStore;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register2);
phone = findViewById(R.id.phone);
optEnter = findViewById(R.id.codeEnter);
countryCodePicker = findViewById(R.id.ccp);
next = findViewById(R.id.nextBtn);
fAuth = FirebaseAuth.getInstance();
fStore = FirebaseFirestore.getInstance();
progressBar = findViewById(R.id.progressBar);
state = findViewById(R.id.state);
resend = findViewById(R.id.resendOtpBtn);
resend.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// todo:: resend OTP
}
});
next.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(!phone.getText().toString().isEmpty() && phone.getText().toString().length() == 8) {
if(!verificationOnProgress){
next.setEnabled(false);
progressBar.setVisibility(View.VISIBLE);
state.setVisibility(View.VISIBLE);
String phoneNum = "+"+countryCodePicker.getSelectedCountryCode()+phone.getText().toString();
Log.d("phone", "Phone No.: " + phoneNum);
requestPhoneAuth(phoneNum);
}else {
next.setEnabled(false);
optEnter.setVisibility(View.GONE);
progressBar.setVisibility(View.VISIBLE);
state.setText("Logging in");
state.setVisibility(View.VISIBLE);
otpCode = optEnter.getText().toString();
if(otpCode.isEmpty()){
optEnter.setError("Required");
return;
}
credential = PhoneAuthProvider.getCredential(verificationId,otpCode);
verifyAuth(credential);
}
}else {
phone.setError("Valid Phone Required");
}
}
});
}
private void requestPhoneAuth(String phoneNumber) {
PhoneAuthProvider.getInstance().verifyPhoneNumber(phoneNumber,60L, TimeUnit.SECONDS,this,
new PhoneAuthProvider.OnVerificationStateChangedCallbacks(){
@Override
public void onCodeAutoRetrievalTimeOut(String s) {
super.onCodeAutoRetrievalTimeOut(s);
Toast.makeText(Register2.this, "OTP Timeout, Please Re-generate the OTP Again.", Toast.LENGTH_SHORT).show();
resend.setVisibility(View.VISIBLE);
}
@Override
public void onCodeSent(String s, PhoneAuthProvider.ForceResendingToken forceResendingToken) {
super.onCodeSent(s, forceResendingToken);
verificationId = s;
token = forceResendingToken;
verificationOnProgress = true;
progressBar.setVisibility(View.GONE);
state.setVisibility(View.GONE);
next.setText("Verify");
next.setEnabled(true);
optEnter.setVisibility(View.VISIBLE);
}
@Override
public void onVerificationCompleted(PhoneAuthCredential phoneAuthCredential) {
// called if otp is automatically detected by the app
verifyAuth(phoneAuthCredential);
}
@Override
public void onVerificationFailed(FirebaseException e) {
Toast.makeText(Register2.this, e.getMessage(), Toast.LENGTH_SHORT).show();
}
});
}
private void verifyAuth(PhoneAuthCredential credential) {
fAuth.signInWithCredential(credential).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if(task.isSuccessful()){
Toast.makeText(Register2.this, "Phone Verified."+fAuth.getCurrentUser().getUid(), Toast.LENGTH_SHORT).show();
checkUserProfile();
}else {
progressBar.setVisibility(View.GONE);
state.setVisibility(View.GONE);
Toast.makeText(Register2.this, "Can not Verify phone and Create Account.", Toast.LENGTH_SHORT).show();
}
}
});
}
@Override
protected void onStart() {
super.onStart();
if(fAuth.getCurrentUser() != null){
progressBar.setVisibility(View.VISIBLE);
state.setText("Checking..");
state.setVisibility(View.VISIBLE);
checkUserProfile();
}
}
private void checkUserProfile() {
DocumentReference docRef = fStore.collection("users").document(fAuth.getCurrentUser().getUid());
docRef.get().addOnSuccessListener(new OnSuccessListener<DocumentSnapshot>() {
@Override
public void onSuccess(DocumentSnapshot documentSnapshot) {
if(documentSnapshot.exists()){
startActivity(new Intent(getApplicationContext(),UserProfil.class));
finish();
}else {
//Toast.makeText(Register.this, "Profile Do not Exists.", Toast.LENGTH_SHORT).show();
startActivity(new Intent(getApplicationContext(),Details.class));
finish();
}
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Toast.makeText(Register2.this, "Profile Do Not Exists", Toast.LENGTH_SHORT).show();
}
});
}
}
更新:抱歉,延迟。我最终找到了它。唯一不对的是,我在登录方法中使用了测试编号。 现在代码可以正常工作了