我正在尝试验证电话号码,但它一直都失败了。我有登录活动,它包含有关用户的所有信息,包括手机号码字段。点击登录按钮后我想打开OTP活动,用户可以在这里输入otp并点击验证按钮验证号码。但它总是进入onVerificationFailed块并显示toast Verification Fail和Invalid Number。
登录活动
public class Login extends AppCompatActivity{
public FirebaseAuth mAuth;
// [END declare_auth]
public String mVerificationId;
PhoneAuthProvider.OnVerificationStateChangedCallbacks mCallbacks;
public PhoneAuthProvider.ForceResendingToken mResendToken;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
btnLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
final String name = txtName.getText().toString();
final String mobileno = txtMobile.getText().toString();
final String email = txtEmail.getText().toString();
final String dob = txtDOB.getText().toString();
final String address = txtAdd.getText().toString();
final String city = txtCity.getText().toString();
final String state = txtState.getText().toString();
final String country = txtCountry.getText().toString();
mCallbacks = new PhoneAuthProvider.OnVerificationStateChangedCallbacks() {
@Override
public void onVerificationCompleted(PhoneAuthCredential phoneAuthCredential) {
Toast.makeText(Login.this,"Verification Done"+ phoneAuthCredential,Toast.LENGTH_LONG).show();
}
@Override
public void onVerificationFailed(FirebaseException e) {
Toast.makeText(Login.this,"Verification Fail",Toast.LENGTH_LONG).show();
if (e instanceof FirebaseAuthInvalidCredentialsException){
Toast.makeText(Login.this, "Invalid Number", Toast.LENGTH_SHORT).show();
}else if (e instanceof FirebaseTooManyRequestsException){
Toast.makeText(Login.this, "Too many Request", Toast.LENGTH_SHORT).show();
}
}
@Override
public void onCodeSent(String s, PhoneAuthProvider.ForceResendingToken forceResendingToken) {
mVerificationId = s;
mResendToken = forceResendingToken;
Toast.makeText(Login.this, "Code Sent", Toast.LENGTH_SHORT).show();
startActivity(new Intent(Login.this, OTP.class));
}
};
PhoneAuthProvider.getInstance().verifyPhoneNumber(mobileno, 60, TimeUnit.SECONDS, Login.this, mCallbacks);
}
}
});
}
private void signInWithPhoneAuthCredential(PhoneAuthCredential credential) {
mAuth.signInWithCredential(credential)
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
// Sign in success, update UI with the signed-in user's informatio
Toast.makeText(Login.this,"Verification done",Toast.LENGTH_LONG).show();
FirebaseUser user = task.getResult().getUser();
// ...
} else {
// Sign in failed, display a message and update the UI
if (task.getException() instanceof FirebaseAuthInvalidCredentialsException) {
// The verification code entered was invalid
Toast.makeText(Login.this,"Verification failed code invalid",Toast.LENGTH_LONG).show();
}
}
}
});
}
}
OTP活动
public class OTP extends AppCompatActivity {
public FirebaseAuth mAuth;
public String mVerificationId;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_otp);
Button btnVerify = (Button) findViewById(R.id.btnVerifyOTP);
final EditText txtOtp = (EditText) findViewById(R.id.txtOtp);
btnVerify.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
PhoneAuthCredential credential = PhoneAuthProvider.getCredential( mVerificationId , txtOtp.getText().toString());
signInWithPhoneAuthCredential(credential);
}
});
}
private void signInWithPhoneAuthCredential(PhoneAuthCredential credential) {
mAuth.signInWithCredential(credential)
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
// Sign in success, update UI with the signed-in user's information
//Log.d(TAG, "signInWithCredential:success");
Toast.makeText(OTP.this,"Verification done",Toast.LENGTH_LONG).show();
FirebaseUser user = task.getResult().getUser();
// ...
} else {
// Sign in failed, display a message and update the UI
//Log.w(TAG, "signInWithCredential:failure", task.getException());
if (task.getException() instanceof FirebaseAuthInvalidCredentialsException) {
// The verification code entered was invalid
Toast.makeText(OTP.this,"Verification failed code invalid",Toast.LENGTH_LONG).show();
}
}
}
});
}
}
答案 0 :(得分:1)
我有同样的问题,我在输入电话号码时说,如果电话号码是79 ..... 6,然后输入为+9179 ... 6(如果是印度电话号码)
根据国家/地区,添加国家/地区代码作为前缀以获得短信代码
希望有帮助
答案 1 :(得分:0)
Firebase会自动验证某些设备上的短信息。在您的情况下,似乎它正在尝试验证短信但不能这样做,这就是为什么无效的电话号码错误,我猜。但是你在测试哪种设备?。
更重要的是,您的代码似乎无法涵盖自动短信验证方案... startActivity(new Intent(Login.this,OTP.class));
您可以在http://www.zoftino.com/firebase-phone-number-authentication-android
检查Firebase电话号码身份验证的完整实施情况