应用程序正常运行,但是我的电话号码上没有身份验证OTP
我在以下代码中犯了任何错误吗?我也已经获得了SMS的许可,并且还具有Internet许可,但是我仍然无法在我的设备上收到OTP消息,我的SIM卡在另一台设备上
public class MainActivity extends AppCompatActivity {
FirebaseAuth mAuth;
EditText editTextPhoneNumber;
EditText editTextOtp;
String codesent;
PhoneAuthProvider.OnVerificationStateChangedCallbacks mCallback;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editTextPhoneNumber = findViewById(R.id.editTextPhoneNumber);
editTextOtp = findViewById(R.id.editTextOTP);
mAuth = FirebaseAuth.getInstance();
mCallback = new PhoneAuthProvider.OnVerificationStateChangedCallbacks() {
@Override
public void onVerificationCompleted(@NonNull PhoneAuthCredential phoneAuthCredential) {
}
@Override
public void onVerificationFailed(@NonNull FirebaseException e) {
}
@Override
public void onCodeSent(@NonNull String s, @NonNull PhoneAuthProvider.ForceResendingToken forceResendingToken) {
super.onCodeSent(s, forceResendingToken);
codesent =s;
Toast.makeText(getApplicationContext(),"Message is sent to the Number",Toast.LENGTH_SHORT).show();
}
};
}
public void sendOtp(View view){
String phoneNumber = editTextPhoneNumber.getText().toString();
PhoneAuthProvider.getInstance().verifyPhoneNumber(
phoneNumber, // Phone number to verify
60, // Timeout duration
TimeUnit.SECONDS, // Unit of timeout
this, // Activity (for callback binding)
mCallback); // OnVerificationStateChangedCallbacks
}
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
Toast.makeText(getApplicationContext(),"Login Successful",Toast.LENGTH_SHORT).show();
// ...
}else{
}
}
});
}
public void verifyMessage(View view){
String code = editTextPhoneNumber.getText().toString();
if (!codesent.equals("")){
verifyCodeSent(codesent,code);
}
}
public void verifyCodeSent(String verifyCode,String code){
PhoneAuthCredential credential = PhoneAuthProvider.getCredential(verifyCode,code);
signInWithPhoneAuthCredential(credential);
}
}
答案 0 :(得分:1)
您应该问自己一些问题:
您是否正在使用模拟器?
第一次实现了代码,并收到包含以下内容的确认消息: 数字?
我将回答我的问题,然后尝试告诉我的内容:
如果您的回答是肯定的,则Firebase会为您提供一条消息(如果您留在同一部手机上),以缩短同时发送多条消息并降低成本,但是如果您不发送消息,他会检查此过程而没有消息代码,并在禁止拨打电话号码时执行所需的操作。
我想问你第二个问题,这样事情可以 将来会澄清。
尝试一下,您可以受益:
PhoneAuthProvider.OnVerificationStateChangedCallbacks mCallbacks;
PhoneAuthProvider.ForceResendingToken mResendToken;
String numberPhone, mVerificationId;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
numberPhone = editTextPhoneNumber.getText().toString();
PhoneAuthProvider.getInstance().verifyPhoneNumber(
numberPhone, // Phone number to verify
60, // Timeout duration
TimeUnit.SECONDS, // Unit of timeout
activity_main.this, // Activity (for callback binding)
mCallbacks);
mCallbacks = new PhoneAuthProvider.OnVerificationStateChangedCallbacks() {
@Override
public void onVerificationCompleted(@NonNull PhoneAuthCredential phoneAuthCredential) {
signInWithPhoneAuthCredential(phoneAuthCredential);
}
@Override
public void onVerificationFailed(@NonNull FirebaseException e) {
}
@Override
public void onCodeSent(@NonNull String s, @NonNull
PhoneAuthProvider.ForceResendingToken forceResendingToken) {
mVerificationId = s;
mResendToken = forceResendingToken;
}
};
}
private void signInWithPhoneAuthCredential(PhoneAuthCredential credential) {
mAuth.signInWithCredential(credential).addOnCompleteListener(this, task -> {
if (task.isSuccessful()) {
// startActivity(new Intent(MainActivity.this, HomeActivity.class));
} else {
Toast.makeText(LoginActivity.this, "There was some error in Login in.", Toast.LENGTH_SHORT).show();
if (task.getException() instanceof FirebaseAuthInvalidCredentialsException) {
Toast.makeText(LoginActivity.this, "The verification code entered was invalid", Toast.LENGTH_SHORT).show();
}
}
});
}