android - Android Firebase 身份验证不接收带有高于 11.8.0 的 SDK 的 SMS
问题描述
我正在实现使用 Firebase 对我的用户进行身份验证的 Android 应用,
我的问题是,从以下位置更改 Gradle 依赖项后:
implementation 'com.google.firebase:firebase-auth:11.8.0'
对于任何更高的SDK 版本,我的代码停止发送 SMS 身份验证消息..
我的代码:
public class AuthenticationActivity extends AppCompatActivity {
private PhoneAuthProvider.OnVerificationStateChangedCallbacks mCallbacks;
private EditText phoneNum;
private PinView verifyCodeET;
private String mVerificationId;
private FirebaseAuth mAuth;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_authentication);
mAuth = FirebaseAuth.getInstance();
Button sendCodeButton = findViewById(R.id.submit1);
Button verifyCodeButton = findViewById(R.id.submit2);
phoneNum = findViewById(R.id.phonenumber);
verifyCodeET = findViewById(R.id.pinView);
sendCodeButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String phoneNumber = phoneNum.getText().toString();
PhoneAuthProvider.getInstance().verifyPhoneNumber(
phoneNumber, // Phone number to verify
60, // Timeout duration
TimeUnit.SECONDS, // Unit of timeout
AuthenticationActivity.this, // Activity (for callback binding)
mCallbacks); // OnVerificationStateChangedCallbacks
}
});
mCallbacks = new PhoneAuthProvider.OnVerificationStateChangedCallbacks() {
@Override
public void onVerificationCompleted(PhoneAuthCredential phoneAuthCredential) {
signInWithPhoneAuthCredential(phoneAuthCredential);
}
@Override
public void onVerificationFailed(FirebaseException e) {
}
@Override
public void onCodeSent(String verificationId,PhoneAuthProvider.ForceResendingToken token) {
mVerificationId = verificationId;
}
};
verifyCodeButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String verificationCode = verifyCodeET.getText().toString();
boolean success = true;
PhoneAuthCredential credential = null;
try {
credential = PhoneAuthProvider.getCredential(mVerificationId, verificationCode);
} catch (Exception e) {
// "Something went wrong"
success = false;
}
if (success) {
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()) {
// Sign in success
} else {
// "Invalid verification code"
}
}
});
}
}
当前 16.1.0 到 12.0.0 之间的任何可用 SDK 版本都会发生这种情况,
一旦我改回 11.8.0,一切都会按预期工作......
任何帮助将不胜感激
.
编辑
在 Nouman Ch 的帮助下,我在 onVerificationFailed() 添加了打印并看到了错误消息:
com.google.firebase.auth.FirebaseAuthInvalidCredentialsException:提供的电话号码格式不正确。请以可解析为 E.164 格式的格式输入电话号码。E.164 电话号码的格式为 [+][国家代码][用户号码,包括区号]。[ 无效的格式。]
使用这里的答案
我已添加到 Gradle 文件中:
implementation 'com.googlecode.libphonenumber:libphonenumber:7.0.4'
implementation 'com.google.firebase:firebase-core:16.0.7'
implementation 'com.google.firebase:firebase-auth:16.1.0'
Java 包括:
import com.google.i18n.phonenumbers.Phonenumber.PhoneNumber;
import com.google.i18n.phonenumbers.NumberParseException;
import com.google.i18n.phonenumbers.PhoneNumberUtil;
并将 onClicK() 函数编辑为:
@Override
public void onClick(View view) {
String phoneNumber = phoneNum.getText().toString();
// Check phoneNumber.length()
PhoneNumberUtil phoneUtil = PhoneNumberUtil.getInstance();
try {
PhoneNumber numberProto = phoneUtil.parse(phoneNumber, "US");
PhoneAuthProvider.getInstance().verifyPhoneNumber(
phoneUtil.format(numberProto, PhoneNumberUtil.PhoneNumberFormat.E164), // Phone number to verify
60, // Timeout duration
TimeUnit.SECONDS, // Unit of timeout
AuthenticationActivity.this, // Activity (for callback binding)
mCallbacks); // OnVerificationStateChangedCallbacks
} catch (NumberParseException e) {
// Wrong format
}
}
现在一切都可以使用最新的 SDK 版本
解决方案
尝试e.printStackTrace();
检查您遇到的错误。使用下面的代码:
mCallbacks = new PhoneAuthProvider.OnVerificationStateChangedCallbacks() {
@Override
public void onVerificationCompleted(PhoneAuthCredential phoneAuthCredential) {
signInWithPhoneAuthCredential(phoneAuthCredential);
}
@Override
public void onVerificationFailed(FirebaseException e) {
e.printStackTrace();
}
@Override
public void onCodeSent(String verificationId,PhoneAuthProvider.ForceResendingToken token) {
mVerificationId = verificationId;
}
};
希望这对你有用。
推荐阅读
- envoyproxy - Envoy 有关网络流量的详细统计信息
- python - 如何解决这个无限递归错误?
- python-3.x - 如何拆分由正则表达式询问产生的数据框列
- json - 需要从 json 中提取密钥并存储在文件中。jmeter 的新功能。而且密钥是动态的。使用 json jmes 提取需要将结果存储到文件中
- javascript - 此反应收集过滤器始终返回 1
- python - 折叠在 python dash 中动态创建的 dbc.card
- nginx - Gitlab 在 GDK 上运行良好,但在生产/暂存时引发 502 错误
- amazon-web-services - 如何跨多个区域创建 vpc?
- jpa - JPA ManyToOne 不同服务器上的不同实例
- associations - python中的Apriori调用问题