gpt4 book ai didi

Android Firebase Phone Auth 第二次没有收到短信

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:43:45 25 4
gpt4 key购买 nike

您好,我正在尝试使用 firebase 在 android 中进行手机身份验证。我第一次安装该应用程序时,短信来了并且验证成功,但随后短信不再出现。我已经从 firebase 的身份验证中删除了用户,但它仍然无法正常工作。

以下是我的代码。

主 Activity .java

public class MainActivity extends AppCompatActivity {

CountryCodePicker ccp;

EditText editTextPhone, editTextCode;

FirebaseAuth mAuth;

String codeSent;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

mAuth = FirebaseAuth.getInstance();

editTextCode = findViewById(R.id.editTextCode);
editTextPhone = findViewById(R.id.editTextPhone);

findViewById(R.id.buttonGetVerificationCode).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
sendVerificationCode();
}
});

findViewById(R.id.buttonSignIn).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
verifySignInCode();
}
});
}

private void verifySignInCode() {
String code = editTextCode.getText().toString();
PhoneAuthCredential credential = PhoneAuthProvider.getCredential(codeSent, code);
signInWithPhoneAuthCredential(credential);
}

private void signInWithPhoneAuthCredential(PhoneAuthCredential credential) {
mAuth.signInWithCredential(credential)
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
//here you can open new activity
Toast.makeText(getApplicationContext(),
"Login Successfull", Toast.LENGTH_LONG).show();
} else {
if (task.getException() instanceof FirebaseAuthInvalidCredentialsException) {
Toast.makeText(getApplicationContext(),
"Incorrect Verification Code ", Toast.LENGTH_LONG).show();
}
}
}
});
}

private void sendVerificationCode() {

String phone = editTextPhone.getText().toString();

if (phone.isEmpty()) {
editTextPhone.setError("Phone number is required");
editTextPhone.requestFocus();
return;
}

if (phone.length() < 6 || phone.length() > 13) {
editTextPhone.setError("Please enter a valid phone");
editTextPhone.requestFocus();
return;
}

PhoneAuthProvider.getInstance().verifyPhoneNumber(
phone, // Phone number to verify
60, // Timeout duration
TimeUnit.SECONDS, // Unit of timeout
this, // Activity (for callback binding)
mCallbacks); // OnVerificationStateChangedCallbacks

Toast.makeText(MainActivity.this,
"SMS Sent, Please Wait....", Toast.LENGTH_LONG).show();

editTextCode.requestFocus();

}

PhoneAuthProvider.OnVerificationStateChangedCallbacks mCallbacks = 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);

codeSent = s;
}
};

}

编辑重启手机允许在上面函数中设置的 60 秒超时后重新发送短信。所以手机似乎在内存中保留了一些东西。

最佳答案

就我而言,我有我的电话号码

Phone numbers for testing (optional)

从那里删除它,firebase 开始向我的号码发送短信代码。 Spark Plan 每月提供 10k 电话授权。这样您就可以在不支付一分钱的情况下测试和部署您的应用程序。

关于Android Firebase Phone Auth 第二次没有收到短信,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50538294/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com