简体   繁体   English

实现 Firebase 电话身份验证时出现 UserCancellationException

[英]UserCancellationException while implementing Firebase Phone Authentication

I am having a problem trying to implement the Firebase phone Authentication for the first time,I am using the FirebaseUI to do this,but every time I run the App on my phone (Huewei y9 2019) and put my number and click on the verify phone number I get a null response without pressing the back button and it keeps sending me back to the sign in page.我在第一次尝试实现 Firebase 手机身份验证时遇到问题,我使用 FirebaseUI 来执行此操作,但每次我在手机上运行应用程序(Huewei y9 2019)并输入我的号码并单击验证电话号码 我在没有按返回按钮的情况下收到 null 响应,它不断将我发送回登录页面。

I have read the documentation and many subjects about implementing the phone Authentication through the FirebaseUI and I have done all the steps including adding the phone in my method in my Firebase Authentication SignIn methods,adding the SHA-1 and SHA-256 to my Firebase project,Downloading the Google-Services.json file and adding it to my project and also I tried enabling/disenabling the Android Device Check API to enable the Safteynet for my app but nothing changes.我已经阅读了有关通过 FirebaseUI 实现电话身份验证的文档和许多主题,并且我已经完成了所有步骤,包括在我的 Firebase 身份验证登录方法中将电话添加到我的方法中,将 SHA-1 和 SHA-256 添加到我的 Firebase 项目中,下载 Google-Services.json 文件并将其添加到我的项目中,并且我尝试启用/禁用 Android 设备检查 API 以启用我的应用程序但没有任何更改。

I got to say that tried to implement Google,Twitter and Email authentication using the FirebaseUI and everything is working fine except for the Phone Authentication.我得说尝试使用 FirebaseUI 实现 Google、Twitter 和 Email 身份验证,除了电话身份验证外,一切正常。

I hope to find someone that can help me to understand this problem and solve it.我希望找到可以帮助我理解这个问题并解决它的人。

Here is my Main Activity code:这是我的主要活动代码:

package com.development.myfirebaseapp;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.constraintlayout.widget.ConstraintSet;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import com.firebase.ui.auth.AuthUI;
import com.firebase.ui.auth.IdpResponse;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import java.util.Arrays;
import java.util.List;



public class MainActivity extends AppCompatActivity {

final Context context = this;

List<AuthUI.IdpConfig> providers;

private final static int SIGN_IN_INTENT_REQUESTCODE = 1;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    createSignInMethods();
}

public void createSignInMethods()
{
            providers = Arrays.asList(
            new AuthUI.IdpConfig.PhoneBuilder().build(),
            new AuthUI.IdpConfig.EmailBuilder().build(),
            new AuthUI.IdpConfig.GoogleBuilder().build(),
            new AuthUI.IdpConfig.TwitterBuilder().build());

    startActivityForResult(AuthUI.getInstance().createSignInIntentBuilder()
            .setAvailableProviders(providers)
            .build(), SIGN_IN_INTENT_REQUESTCODE);

}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
    super.onActivityResult(requestCode,resultCode,data);

    if(requestCode == SIGN_IN_INTENT_REQUESTCODE)
    {
        IdpResponse response = IdpResponse.fromResultIntent(data);

        if(resultCode == RESULT_OK)
        {
            FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();

            Toast.makeText(context,"Successfully signed in",Toast.LENGTH_SHORT).show();

        }
        else
        {
            int errorCode = response.getError().getErrorCode();

            Log.d("authErrorCode",String.valueOf(errorCode));

            Toast.makeText(context,"Failed to sign in",Toast.LENGTH_SHORT).show();
        }
    }
 }
    
}

And here is my App Build.gradle file:这是我的应用程序 Build.gradle 文件:

apply plugin: 'com.android.application'
apply plugin: 'com.google.gms.google-services'

android {
compileSdkVersion 29
buildToolsVersion "29.0.2"

defaultConfig {
    applicationId "com.development.myfirebaseapp"
    minSdkVersion 18
    targetSdkVersion 29
    versionCode 1
    versionName "1.0"

    testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}

buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
    }
 }

}

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])

implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'

implementation platform('com.google.firebase:firebase-bom:26.5.0')
implementation 'com.firebaseui:firebase-ui-auth:6.4.0'
implementation 'com.twitter.sdk.android:twitter-core:3.1.1'
}

Finally here is my log file:最后这是我的日志文件:

2021-03-04 05:03:29.600 20159-20159/? E/AuthUI: A sign-in error occurred.
com.firebase.ui.auth.data.model.UserCancellationException: Unknown error
    at com.firebase.ui.auth.data.remote.PhoneSignInHandler.onActivityResult(PhoneSignInHandler.java:39)
    at com.firebase.ui.auth.ui.idp.AuthMethodPickerActivity.onActivityResult     (AuthMethodPickerActivity.java:383)
    at android.app.Activity.dispatchActivityResult(Activity.java:7797)
    at android.app.ActivityThread.deliverResults(ActivityThread.java:5071)
    at android.app.ActivityThread.performResumeActivity(ActivityThread.java:4409)
    at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:4470)
    at android.app.servertransaction.ResumeActivityItem.execute(ResumeActivityItem.java:51)
    at android.app.servertransaction.TransactionExecutor.executeLifecycleState     (TransactionExecutor.java:145)
    at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:70)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2199)
    at android.os.Handler.dispatchMessage(Handler.java:112)
    at android.os.Looper.loop(Looper.java:216)
    at android.app.ActivityThread.main(ActivityThread.java:7625)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:524)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:987)

Just in case anyone faced this problem again I updated the Firebase Bom to the latest version (26.6.0) and the FirebaseUI Auth to version (7.1.1) and finally my problem solved以防万一有人再次遇到这个问题,我将 Firebase Bom 更新到最新版本(26.6.0)和 FirebaseUI Auth 到版本(7.1.1),最后我的问题解决了

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM