我正在使用 Firebase PhoneAuth 来授权用户。它的作用是自动触发成功回调并且不返回代码,即使它向我发送了 SMS。我需要更新我的 UI 中的 SMS 代码,因为我在任何地方都没有收到它,或者我认为我在这里遗漏了一些东西。
这是我的代码:
public class OTPActivity extends Activity {
private TextView tvTimer;
private int timeCount = 30;
private ImageButton ibBack;
private FirebaseAuth mAuth;
private String mVerificationId;
private PhoneAuthProvider.ForceResendingToken mResendToken;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_o_t_p);
Constants.removeStatusBar(this);
ibBack = findViewById(R.id.ibBack);
tvTimer = findViewById(R.id.tvTimer);
String phoneNumber = "+91" + getIntent().getStringExtra("phone");
Handler h = new Handler();
h.postDelayed(new Runnable() {
@Override
public void run() {
if (timeCount > 0) {
tvTimer.setText("You can resend OTP after 00:" + timeCount + "s");
timeCount--;
h.postDelayed(this, 1000);
}
}
}, 0);
ibBack.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onBackPressed();
}
});
mAuth = FirebaseAuth.getInstance();
PhoneAuthProvider.OnVerificationStateChangedCallbacks mCallbacks = new PhoneAuthProvider.OnVerificationStateChangedCallbacks() {
@Override
public void onVerificationCompleted(PhoneAuthCredential credential) {
// This callback will be invoked in two situations:
// 1 - Instant verification. In some cases the phone number can be instantly
// verified without needing to send or enter a verification code.
// 2 - Auto-retrieval. On some devices Google Play services can automatically
// detect the incoming verification SMS and perform verification without
// user action.
Log.d(Constants.TAG, "onVerificationCompleted:" + credential);
signInWithPhoneAuthCredential(credential);
}
@Override
public void onVerificationFailed(FirebaseException e) {
// This callback is invoked in an invalid request for verification is made,
// for instance if the the phone number format is not valid.
Log.w(Constants.TAG, "onVerificationFailed", e);
if (e instanceof FirebaseAuthInvalidCredentialsException) {
// Invalid request
} else if (e instanceof FirebaseTooManyRequestsException) {
// The SMS quota for the project has been exceeded
}
// Show a message and update the UI
}
@Override
public void onCodeSent(@NonNull String verificationId,
@NonNull PhoneAuthProvider.ForceResendingToken token) {
// The SMS verification code has been sent to the provided phone number, we
// now need to ask the user to enter the code and then construct a credential
// by combining the code with a verification ID.
Log.d(Constants.TAG, "onCodeSent:" + verificationId);
// Save verification ID and resending token so we can use them later
mVerificationId = verificationId;
mResendToken = token;
}
};
PhoneAuthOptions options =
PhoneAuthOptions.newBuilder(mAuth)
.setPhoneNumber(phoneNumber) // Phone number to verify
.setTimeout(60 L, TimeUnit.SECONDS) // Timeout and unit
.setActivity(this) // Activity (for callback binding)
.setCallbacks(mCallbacks) // OnVerificationStateChangedCallbacks
.build();
PhoneAuthProvider.verifyPhoneNumber(options);
}
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(Constants.TAG, "signInWithCredential:success");
FirebaseUser user = task.getResult().getUser();
// Update UI
Intent intent = new Intent(OTPActivity.this, SignUpActivity.class);
startActivity(intent);
} else {
// Sign in failed, display a message and update the UI
Log.w(Constants.TAG, "signInWithCredential:failure", task.getException());
if (task.getException() instanceof FirebaseAuthInvalidCredentialsException) {
// The verification code entered was invalid
Toast.makeText(OTPActivity.this, task.getException().getMessage(), Toast.LENGTH_SHORT).show();
}
}
}
});
}
}