Yog*_*ann 5 android firebase firebase-authentication
我正在实现使用 Firebase 来验证我的用户身份的 Android 应用程序,
我的问题是,在更改 Gradle 依赖项后:
implementation 'com.google.firebase:firebase-auth:11.8.0'
Run Code Online (Sandbox Code Playgroud)
对于任何更高的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"
}
}
});
}
}
Run Code Online (Sandbox Code Playgroud)
当前 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'
Run Code Online (Sandbox Code Playgroud)
Java 包括:
import com.google.i18n.phonenumbers.Phonenumber.PhoneNumber;
import com.google.i18n.phonenumbers.NumberParseException;
import com.google.i18n.phonenumbers.PhoneNumberUtil;
Run Code Online (Sandbox Code Playgroud)
并将 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
}
}
Run Code Online (Sandbox Code Playgroud)
现在一切都可以使用最新的 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;
}
};
Run Code Online (Sandbox Code Playgroud)
希望这对你有用。
| 归档时间: |
|
| 查看次数: |
4563 次 |
| 最近记录: |