Вот код OTP для отправки:
Код: Выделить всё
public void sendOTP(){
final String Mobile = getMobileNo();
//Looks if the mobile number is already signed
FirebaseUser currentUser = FirebaseAuth.getInstance().getCurrentUser();
if(currentUser!=null){
Toast.makeText(this,"User with mobile no"+Mobile+" already exists",Toast.LENGTH_LONG).show();
Intent in = new Intent(OTPActivity.this,LoginActivity.class);
in.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(in);
}
//if not a current user then send otp.
else {
PhoneAuthOptions options = PhoneAuthOptions.newBuilder(mAuth)
.setPhoneNumber(Mobile)
.setTimeout(120L, TimeUnit.SECONDS)
.setActivity(this)
.setCallbacks(mCallBack)
.build();
PhoneAuthProvider.verifyPhoneNumber(options);
}
}
Код: Выделить всё
private PhoneAuthProvider.OnVerificationStateChangedCallbacks mCallBack = new PhoneAuthProvider.OnVerificationStateChangedCallbacks() {
@Override
public void onCodeSent(@NonNull String s, @NonNull PhoneAuthProvider.ForceResendingToken forceResendingToken) {
super.onCodeSent(s, forceResendingToken);
verificationId = s;
setVerificationId(verificationId);
setToken(forceResendingToken);
}
@Override
public void onVerificationCompleted(@NonNull PhoneAuthCredential phoneAuthCredential) {
final String code = phoneAuthCredential.getSmsCode();
if(code!=null){
OTP.setText(code);
verifyCode(getVerificationId(),code);
}
}
@Override
public void onVerificationFailed(@NonNull FirebaseException e) {
Toast.makeText(OTPActivity.this,e.getMessage(),Toast.LENGTH_LONG).show();
}
};
Код: Выделить всё
public void verifyCode(String verificationId,String code){
if(code!=null) {
PhoneAuthCredential credential = PhoneAuthProvider.getCredential(verificationId, code);
FirebaseAuth.getInstance().signInWithCredential(credential)
.addOnCompleteListener(new OnCompleteListener() {
@Override
public void onComplete(@NonNull Task task) {
if(task.isSuccessful()){
FirebaseUser user = task.getResult().getUser();
AlertDialog.Builder builder = new AlertDialog.Builder(OTPActivity.this);
builder.setMessage("Congratulations!!You are Registered. Do You Want To Login?");
builder.setCancelable(true);
builder.setPositiveButton(
"Yes",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Intent in = new Intent(OTPActivity.this, LoginActivity.class);
in.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(in);
}
});
builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(OTPActivity.this, "Exiting the app.", Toast.LENGTH_SHORT).show();
System.exit(0);
}
});
}
else
{
if(task.getException() instanceof FirebaseAuthInvalidCredentialsException){
Toast.makeText(OTPActivity.this,task.getException().getMessage(),Toast.LENGTH_SHORT).show();
}
}
}
});
}
else
{
Toast.makeText(this,"Code Field is Empty.Please Enter OTP.",Toast.LENGTH_LONG).show();
}
}
Срок действия смс-кода истек. Пожалуйста, отправьте код повторно.
хотя я получил код OTP несколько секунд назад.
Подробнее здесь: https://stackoverflow.com/questions/709 ... -exception
Мобильная версия