Firebase Phone Verification verifyPhoneNumber() deprecated + Application Crashed

杀马特。学长 韩版系。学妹 提交于 2020-12-25 07:44:13

问题


Getting error after upgrading Firebase Auth (20.0.0) dependency for Phone Authentication, PhoneAuthProvider.getInstance().verifyPhoneNumber()

Dependency:

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

Error:

java.lang.NoClassDefFoundError: Failed resolution of: Landroidx/browser/customtabs/CustomTabsIntent$Builder;
        at com.google.firebase.auth.internal.RecaptchaActivity.zza(com.google.firebase:firebase-auth@@20.0.0:92)
        at com.google.firebase.auth.api.internal.zzeq.zza(com.google.firebase:firebase-auth@@20.0.0:79)
        at com.google.firebase.auth.api.internal.zzeq.onPostExecute(com.google.firebase:firebase-auth@@20.0.0:88)
        at android.os.AsyncTask.finish(AsyncTask.java:755)
        at android.os.AsyncTask.access$900(AsyncTask.java:192)
        at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:772)
        at android.os.Handler.dispatchMessage(Handler.java:107)
        at android.os.Looper.loop(Looper.java:237)
        at android.app.ActivityThread.main(ActivityThread.java:7948)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1075)
     Caused by: java.lang.ClassNotFoundException: Didn't find class "androidx.browser.customtabs.CustomTabsIntent$Builder"

Can anyone explain what should I change for new dependency? What are the new steps?


回答1:


This is what I did to remove the Error:

I referred firebase phone auth documentation and made the necessary changes:

Replace this:

PhoneAuthProvider.getInstance().verifyPhoneNumber(
            phoneNumber, //phone number to be verified
            60, // validity of the OTP
            TimeUnit.SECONDS,
            (Activity) TaskExecutors.MAIN_THREAD,
            mCallBack // onVerificationStateChangedCallback
    );

With this

PhoneAuthOptions options =
            PhoneAuthOptions.newBuilder(mAuth)
                    .setPhoneNumber(phoneNumber)       // Phone number to verify
                    .setTimeout(60L, TimeUnit.SECONDS) // Timeout and unit
                    .setActivity(this)                 // Activity (for callback binding)
                    .setCallbacks(mCallBack)          // OnVerificationStateChangedCallbacks
                    .build();
    PhoneAuthProvider.verifyPhoneNumber(options);

Also, add this to your app/gradle file dependencies:

implementation 'androidx.browser:browser:1.2.0'

This will help firebase to open the browser for reCAPTCHA verification.

Hope this works!




回答2:


In new Firebase auth version,they've made major changes like Recaptcha for human verification.it needs browser to verify so,Add below depen.and read about changes refer me

 implementation 'androidx.browser:browser:1.2.0'



回答3:


Finally, I got solutions with the help of Alex Mamo's Answer and This Documentation

The steps which I followed:

  1. Update new dependency implementation 'com.google.firebase:firebase-auth:20.0.0'

  2. Update new code:

    For send OTP:

    PhoneAuthProvider.verifyPhoneNumber(
         PhoneAuthOptions
                 .newBuilder(FirebaseAuth.getInstance())
                 .setActivity(this)
                 .setPhoneNumber(phoneNumber)
                 .setTimeout(60L, TimeUnit.SECONDS)
                 .setCallbacks(mCallbacks)
                 .build());
    

    For Resend OTP

    PhoneAuthProvider.verifyPhoneNumber(
             PhoneAuthOptions
                     .newBuilder(FirebaseAuth.getInstance())
                     .setActivity(this)
                     .setPhoneNumber(phoneNumber)
                     .setTimeout(60L, TimeUnit.SECONDS)
                     .setCallbacks(mCallbacks)
                     .setForceResendingToken(token)
                     .build());
    

    Still, you will get an error as below:

[SmsRetrieverHelper] SMS verification code request failed: unknown status code: 17028 A safety_net_token was passed, but no matching SHA-256 was registered in the Firebase console. Please make sure that this application’s packageName/SHA256 pair is registered in the Firebase Console.

  1. You need to copy SHA-256 from your Keystore or JKS file and add here in SHA Certificate fingerprints:

  2. Finally, You did it.

    There is no need for a reCAPTCHA verification.

Thank you.




回答4:


As it is specified in the official documentation of verifyPhoneNumber(String phoneNumber, long timeout, TimeUnit unit, Activity activity, PhoneAuthProvider.OnVerificationStateChangedCallbacks callbacks) method:

This method is deprecated in favor of verifyPhoneNumber(PhoneAuthOptions)

So from now on, to verify a phone number we'll need to call this method and pass a PhoneAuthOptions object as an argument. In my opinion, this is some kind of similar to updateProfile(UserProfileChangeRequest request), where we should pass a PhoneAuthOptions object as an argument.




回答5:


In new Firebase auth version,they've made major changes like Recaptcha , SafetyNet for human verification. I have faced the same issue by adding

implementation "androidx.browser:browser:1.3.0

it solved my problem but user experience was not good firebase was opening browser to verify Recaptcha and that was looking odd in app flow.

Firebase Quote "The reCAPTCHA flow will only be triggered when SafetyNet is unavailable or your device does not pass suspicion checks. Nonetheless, you should ensure that both scenarios are working correctly." So to enable SafetyNet follow below steps or you can also visit https://firebase.google.com/docs/auth/android/phone-auth for info

  1. Go to google cloud console , select your project .

  2. Click on navigation menu and select APis & services and then select Dashboard .

  3. Click on enable api and services and enable api " Android Device Verification".

  4. Add SHA 256 in firebase project settings.(debug and release both)



来源:https://stackoverflow.com/questions/64608484/firebase-phone-verification-verifyphonenumber-deprecated-application-crashed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!