我正在为Android手机创建应用并使用Firebase OTP身份验证,但我不确定代码是否有问题,或者Firebase发送短信确实存在问题,就像我到目前为止所读到的那样。任何帮助将不胜感激!
这是代码:
public class MainActivity extends AppCompatActivity {
Button btSendCode;
Button btResend;
Button btVerify;
EditText etPhoneNumber;
EditText etEnterCode;
TextView tvGoHere;
String phoneVerificationID;
FirebaseAuth auth;
PhoneAuthProvider.OnVerificationStateChangedCallbacks verificationCallbacks;
PhoneAuthProvider.ForceResendingToken resendToken;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btSendCode = (Button) findViewById(R.id.button_sendcode);
btResend = (Button) findViewById(R.id.button_resend);
btVerify = (Button) findViewById(R.id.button_verify);
etPhoneNumber = (EditText) findViewById(R.id.edittext_phonenumber);
etEnterCode = (EditText) findViewById(R.id.edittext_entercode);
tvGoHere = (TextView) findViewById(R.id.textview_gohere);
auth = FirebaseAuth.getInstance();
}
public void clickSendCode(View view) {
etPhoneNumber.setVisibility(View.INVISIBLE);
btSendCode.setVisibility(View.INVISIBLE);
etEnterCode.setVisibility(View.VISIBLE);
btResend.setVisibility(View.VISIBLE);
btVerify.setVisibility(View.VISIBLE);
//firebase code
String phoneNumber = etPhoneNumber.getText().toString();
setUpVerificationCallbacks();
PhoneAuthProvider.getInstance().verifyPhoneNumber(
phoneNumber,
120,
TimeUnit.SECONDS,
this,
verificationCallbacks
);
}
public void setUpVerificationCallbacks() {
verificationCallbacks = new PhoneAuthProvider.OnVerificationStateChangedCallbacks() {
@Override
public void onVerificationCompleted(PhoneAuthCredential credential) {
signInWithPhoneAuthCredential(credential);
}
@Override
public void onVerificationFailed(FirebaseException e) {
if(e instanceof FirebaseAuthInvalidCredentialsException) {
Toast.makeText(MainActivity.this, "Invalid phone format", Toast.LENGTH_SHORT).show();
} else if(e instanceof FirebaseTooManyRequestsException) {
Toast.makeText(MainActivity.this, "Too many requests", Toast.LENGTH_SHORT).show();
}
}
@Override
public void onCodeSent(String verificationID, PhoneAuthProvider.ForceResendingToken token) {
phoneVerificationID = verificationID;
resendToken = token;
}
};
}
public void clickVerifyCode(View view) {
String code = etEnterCode.getText().toString();
PhoneAuthCredential credential = PhoneAuthProvider.getCredential(phoneVerificationID, code);
signInWithPhoneAuthCredential(credential);
}
public void signInWithPhoneAuthCredential(PhoneAuthCredential credential) {
auth.signInWithCredential(credential)
.addOnCompleteListener(new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if(task.isSuccessful()) {
FirebaseUser user = task.getResult().getUser();
} else {
if(task.getException() instanceof FirebaseAuthInvalidCredentialsException) {
Toast.makeText(MainActivity.this, "Invalid code", Toast.LENGTH_SHORT).show();
}
}
}
});
}
public void clickResendCode(View view) {
String phoneNumber = etPhoneNumber.getText().toString();
setUpVerificationCallbacks();
PhoneAuthProvider.getInstance().verifyPhoneNumber(
phoneNumber,
120,
TimeUnit.SECONDS,
this,
verificationCallbacks,
resendToken
);
}
public void onClickGoHere(View view) {
Intent intentLogin = new Intent(MainActivity.this, LoginActivity.class);
startActivity(intentLogin);
}
}
答案 0 :(得分:1)
电话号码必须以+
开头,后跟国家/地区代码,后跟号码。示例:+9195021*****
。并启用手机身份验证签名方法。