Android Firebase身份验证无法通过11.8.0以上的SDK接收短信

问题描述 投票:0回答:1

我正在实施使用Firebase对我的用户进行身份验证的Android应用,

我的问题是在更改Gradle依赖关系后:

implementation 'com.google.firebase:firebase-auth:11.8.0'

对于任何更高的SDK version,我的代码停止发送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电话号码以[+] [国家代码] [用户号码包括区号]格式写入。 [ 无效的格式。 ]

使用here的答案

我已添加到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版本一致

android firebase firebase-authentication
1个回答
1
投票

试试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;
            }
        };

希望这对你有用。

© www.soinside.com 2019 - 2024. All rights reserved.