Android Firebase身份验证未通过11.8.0以上版本的SDK接收SMS

时间:2019-03-11 09:17:19

标签: android firebase firebase-authentication

我正在实施使用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版本

1 个答案:

答案 0 :(得分: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;
            }
        };

希望这对您有用。