【发布时间】:2020-05-11 01:48:26
【问题描述】:
我正在尝试在我的 Android 应用程序中使用 Firebase 身份验证和 Otp,但此代码不起作用。 它没有发送短信,我在评论后提到的吐司部分也没有执行。 包 com.example.esport;
public class otpverification extends AppCompatActivity {
private Button verify;
private EditText code;
private String number,verification_code;
private FirebaseAuth mAuth;
private PhoneAuthProvider.OnVerificationStateChangedCallbacks mcallBack;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_otpverification);
verify =findViewById(R.id.otpverifiaction_verify);
code=findViewById(R.id.otpverification_otp);
number=getIntent().getStringExtra("ph_number");
Toast.makeText(getApplicationContext(),number,Toast.LENGTH_LONG).show();
//这个toast也没有在app上执行 发送短信(号码);
mcallBack = new PhoneAuthProvider.OnVerificationStateChangedCallbacks() {
@Override
public void onVerificationCompleted(PhoneAuthCredential phoneAuthCredential) {
}
@Override
public void onVerificationFailed(FirebaseException e) {
}
@Override
public void onCodeSent(String s, PhoneAuthProvider.ForceResendingToken forceResendingToken) {
super.onCodeSent(s, forceResendingToken);
verification_code=s;
}
};
}
private void sendsms(String number) {
PhoneAuthProvider.getInstance().verifyPhoneNumber(
number,600, TimeUnit.SECONDS, TaskExecutors.MAIN_THREAD,mcallBack
);
}
public void verify(View view){
String input_code=code.getText().toString();
if(verification_code.equals(""))
{
verifyPhoneNumber(verification_code,input_code);
}
}
private void verifyPhoneNumber(String verification_code, String input_code) {
PhoneAuthCredential credential = PhoneAuthProvider.getCredential(verification_code,input_code);
signInWithPhone(credential);
}
public void signInWithPhone(PhoneAuthCredential credential){
mAuth.signInWithCredential(credential).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if(task.isSuccessful())
{
Toast.makeText(getApplicationContext(),"Sucessful",Toast.LENGTH_SHORT).show();
}
}
});
}
}
【问题讨论】:
-
确保您使用国家/地区代码发送短信,例如在号码中添加 +91 表示印度。
-
已经添加国家代码
-
好的,在
mCallback = new PhoneAuthProvider.OnVerificationStateChangedCallbacks() {}之后调用sendSms(number)
标签: android firebase authentication firebase-authentication one-time-password