firebase phone auth android崩溃

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了firebase phone auth android崩溃相关的知识,希望对你有一定的参考价值。

所以我正在使用firebase数据库和Auth,我正在尝试构建Phone Auth但由于某种原因应用程序崩溃..不知道为什么..我是新的编程与android工作室和Firebase数据库上的第一个计时器,不知道为什么应用程序崩溃,但它崩溃后,我按下一个激活verifySignInCode()insta崩溃的按钮后,它确实发送一个代码为电子邮件的邮件错误在这里

09-15 20:39:41.804 22046-22046/com.example.erelpc.calltest E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.erelpc.calltest, PID: 22046
    java.lang.IllegalArgumentException: Cannot create PhoneAuthCredential without either verificationProof, sessionInfo, ortemprary proof.
        at com.google.android.gms.common.internal.Preconditions.checkArgument(Unknown Source:8)
        at com.google.firebase.auth.PhoneAuthCredential.<init>(Unknown Source:6)
        at com.google.firebase.auth.PhoneAuthProvider.getCredential(Unknown Source:33)
        at com.example.erelpc.calltest.MainActivity.verifySignInCode(MainActivity.java:92)
        at com.example.erelpc.calltest.MainActivity.access$100(MainActivity.java:28)
        at com.example.erelpc.calltest.MainActivity$2.onClick(MainActivity.java:61)
        at android.view.View.performClick(View.java:6877)

这是MainActivity

    /// SMS Handler
    private void sendVerificationCode(){
        String phone = etphonenumber.getText().toString();
        if (phone.isEmpty()) {
            etphonenumber.setError("Enter a Phone Number!");
            etphonenumber.requestFocus();
            return;
        }
        if (phone.length() != 9){
            etphonenumber.setError("Please Enter a valid Number!");
            etphonenumber.requestFocus();
            return;
        }
        phone = "+972" + phone;
        Intent intent =  new Intent(this, loginsuccess.class);
            PhoneAuthProvider.getInstance().verifyPhoneNumber(
                phone,        // Phone number to verify
                60,                 // Timeout duration
                TimeUnit.SECONDS,   // Unit of timeout
                    this,               // Activity (for callback binding)
                    mCallbacks);        // OnVerificationStateChangedCallbacks
    }
    private void verifySignInCode(){

        String code = etcodeveri.getText().toString();
        PhoneAuthCredential credential = PhoneAuthProvider.getCredential(codesent, code);
        signInWithPhoneAuthCredential(credential);
    }

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


    private void signInWithPhoneAuthCredential(PhoneAuthCredential credential) {
        mAuth.signInWithCredential(credential)
                .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
                    @Override
                    public void onComplete(@NonNull Task<AuthResult> task) {
                        if (task.isSuccessful()) {
                            Toast.makeText(getApplicationContext(), "Login Success!", Toast.LENGTH_SHORT).show();
                            registerModule();
                        } else {
                            if (task.getException() instanceof FirebaseAuthInvalidCredentialsException) {
                                Toast.makeText(getApplicationContext(), "Login Unsuccess!", Toast.LENGTH_SHORT).show();
                            }
                        }
                    }
                });
    }  
    public void registerModule(){
        Intent intent =  new Intent(this, loginsuccess.class);
        startActivity(intent);
    }

按下“btnAdd”后发生崩溃

答案

该应用程序崩溃,因为该方法没有获得有效的credential使phoneAuth工作。这可以通过在try方法上使用catchsignInWithPhoneAuthCredential()来解决。

在代码中它看起来像这样:

private void verifyCode(){

        String code = cd.getText().toString(); // this is OTP
        String pH = phone.getText().toString(); // this is phone number
        if(code.equals("") && pH.equals(""))
            Toast.makeText(MainActivity.this,"Nothing to validate", Toast.LENGTH_SHORT).show();
        else {
            try {
                PhoneAuthCredential credential = PhoneAuthProvider.getCredential(otp, code);
                signInWithPhoneAuthCredential(credential);
            }
            catch (Exception e) {
                Log.i("exception",e.toString());
                Toast.makeText(MainActivity.this,"Invalid credentials",Toast.LENGTH_LONG).show();
            }

        }
    }

此外,你应该放置更多的catchif语句,以避免在方法中给出null值。

以上是关于firebase phone auth android崩溃的主要内容,如果未能解决你的问题,请参考以下文章

Firebase Phone Auth

Firebase Phone Auth 在发布版本中不起作用

React-Native Firebase Phone Auth 冻结 iPhone

Flutter-Firebase phone Auth 总是在 iOS 上返回 Token mismatch

Firebase Phone Auth 不会用我的代码向我发送短信

Flutter Web 中的 Firebase Phone 身份验证