当前位置: 首页 > 知识库问答 >
问题:

我在电话认证系统中没有收到firebase的Otp

东方俊力
2023-03-14

我基本上创建了2个Xml。一个是用户输入电话号码,然后按下按钮,它会转到OTP验证屏幕。但这里的问题是我没有收到来自Firebase的任何OTP,但5秒后我收到一条敬酒消息,验证失败。如果我甚至没有收到消息,它怎么会失败。我还尝试输入另一个号码,以便我可以手动输入代码。但是Firebase没有向我发送OTP。

基本实现---点击忘记密码-

另外,我已经在Firebase中启用了电话身份验证。

Ottpactivity。JAVA

public class VerifyOtp extends AppCompatActivity {

    public String NumberEnteredByUser,verificationCodeBySystem;
    Button VerifyButton;
    PinView phoneEnteredByUser;
    FirebaseAuth auth;
    PhoneAuthProvider.OnVerificationStateChangedCallbacks mCallback;



    

    @RequiresApi(api = Build.VERSION_CODES.N)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_verify_otp);
        getSupportActionBar().hide();

        Intent intent =getIntent();
        NumberEnteredByUser = intent.getStringExtra("phoneNo");



        VerifyButton = findViewById(R.id.btnVerify);
        phoneEnteredByUser = findViewById(R.id.EnterCode);
        auth = FirebaseAuth.getInstance();

        send_code_to_user(NumberEnteredByUser);




        VerifyButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                checkcode();
            }
        });

    }


    private void checkcode() {
        String userEnteredOtp = phoneEnteredByUser.getText().toString();
        if(userEnteredOtp.isEmpty() || userEnteredOtp.length()<6){
            Toast.makeText(this, "Wrong Otp!", Toast.LENGTH_SHORT).show();
            return;
        }
        finishEverything(userEnteredOtp);
    }

    private void finishEverything(String code) {
        phoneEnteredByUser.setText(code);
        PhoneAuthCredential credential = PhoneAuthProvider.getCredential(verificationCodeBySystem,code);
        sign_in(credential);
    }

    private void sign_in(PhoneAuthCredential credential) {
        FirebaseAuth auth = FirebaseAuth.getInstance();
        auth.signInWithCredential(credential).addOnCompleteListener(VerifyOtp.this, new OnCompleteListener<AuthResult>() {
            @Override
            public void onComplete(@NonNull Task<AuthResult> task) {
                if(task.isSuccessful())
                {
                    Toast.makeText(VerifyOtp.this, "UserSignedInSuccessfully", Toast.LENGTH_SHORT).show();
                    startActivity(new Intent(getApplicationContext(),ResetPassword.class));
                }
                else {
                    Toast.makeText(VerifyOtp.this,task.getException().getMessage(), Toast.LENGTH_SHORT).show();
                }
            }
        });

    }

    private void send_code_to_user(String NumberEnteredByUser ) {
        PhoneAuthOptions options =
                PhoneAuthOptions.newBuilder(auth)
                        .setPhoneNumber(NumberEnteredByUser)       // Phone number to verify
                        .setTimeout(60L, TimeUnit.SECONDS) // Timeout and unit
                        .setActivity(this)                 // Activity (for callback binding)
                        .setCallbacks(new PhoneAuthProvider.OnVerificationStateChangedCallbacks() {

                            @Override
                            public void onVerificationCompleted(PhoneAuthCredential phoneAuthCredential) {
                                Toast.makeText(VerifyOtp.this, "Verification Completed", Toast.LENGTH_SHORT).show();
                                String code = phoneAuthCredential.getSmsCode();
                                if (code != null) {
                                    finishEverything(code);
                                }
                            }

                            @Override
                            public void onVerificationFailed(FirebaseException e) {
                                Toast.makeText(VerifyOtp.this, "Verification Failed", Toast.LENGTH_SHORT).show();
                            }

                            @Override
                            public void onCodeSent(String s, PhoneAuthProvider.ForceResendingToken forceResendingToken) {
                                super.onCodeSent(s, forceResendingToken);
                                verificationCodeBySystem = s;
                                Toast.makeText(VerifyOtp.this, "Code sent", Toast.LENGTH_SHORT).show();
                            }
                        })          // OnVerificationStateChangedCallbacks
                        .build();
        PhoneAuthProvider.verifyPhoneNumber(options);

    }
}

构建Gradle项目

// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
    repositories {
        google()
        jcenter()
    }
    dependencies {
        classpath "com.android.tools.build:gradle:4.1.2"
        classpath 'com.google.gms:google-services:4.3.8'

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        google()
        jcenter()
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

构建Gradle应用程序

plugins {
    id 'com.android.application'
    id 'com.google.gms.google-services'


}

android {
    compileSdkVersion 30
    buildToolsVersion "30.0.3"

    defaultConfig {
        applicationId "com.mitadt.newui"
        minSdkVersion 16
        targetSdkVersion 30
        versionCode 1
        versionName "1.0"
        multiDexEnabled true

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

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

dependencies {

    implementation 'androidx.appcompat:appcompat:1.2.0'
    implementation platform('com.google.firebase:firebase-bom:28.0.1')
    implementation 'com.google.firebase:firebase-analytics'
    implementation 'com.google.android.material:material:1.3.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
    implementation 'androidx.navigation:navigation-fragment:2.2.2'
    implementation 'androidx.navigation:navigation-ui:2.2.2'
    implementation 'com.google.firebase:firebase-firestore:23.0.0'
    testImplementation 'junit:junit:4.+'
    androidTestImplementation 'androidx.test.ext:junit:1.1.2'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
    implementation 'com.google.firebase:firebase-database:19.7.0'
    implementation 'com.google.firebase:firebase-auth:19.3.2'
    implementation 'com.android.support:multidex:1.0.3'
    //OTP VIEW DEPENDENCY
    implementation 'com.chaos.view:pinview:1.4.3'
}

请帮帮我。谢谢!!!

共有2个答案

孟翰藻
2023-03-14

在用户电话号码前添加国家代码(印度为91)。它会起作用的。

NumberEnteredByUser = "91"+intent.getStringExtra("phoneNo");   
湛同
2023-03-14

验证失败,因为你没有启用应用验证。为此,您可以按照以下步骤操作:

>

  • 转到谷歌云控制台,选择你的项目并启用Android设备验证。以下是直接链接:https://console.cloud.google.com/apis/library/androidcheck.googleapis.com

    现在,在app level grade中添加浏览器依赖项,因为应用需要从浏览器打开验证码验证页面:

     implementation 'androidx.browser:browser:1.3.0' 
    

    然后,在Android Studio顶部菜单中构建

    现在,你可以走了。

    注意:你的应用程序可能会显示验证码验证网页

  •  类似资料:
    • 我遵循了Flatter fire文档来实现firebase phone auth。我在firebase_auth的早期版本中尝试了这一点,一切似乎都正常,但我启动了一个新的应用程序,并安装了最新版本的firebase_auth,每当我调用firebase verifyNumber方法时,我的应用程序都会显示以下日志: 我的应用程序崩溃的原因可能是什么? 注意:

    • 我试图用手机登录。每次我输入手机号码时,它都会显示这些错误消息并退出应用程序。我使用了https://firebase.flutter.dev/docs/auth/phone留档中的代码。代码几乎与留档相同。 我正在为Java/Flatter/Firebase sdk等所有东西使用更新版本。我还在Firebase应用程序中添加了SHA1密钥,并启用了手机认证。我还尝试了物理和虚拟设备。但每次都是同

    • 我是一个新的flutter,并使用firebase验证电话号码,但我无法验证。我浏览了它,但没有得到任何满意的解决办法。 我从StackOverflow实现了一些代码,但出现异常“Unhanded Exception:type”(FirebaseUser)=>Null'不是type Cast“中类型”(AuthCredential)=>Void“的子类型,因此无法捕获该异常。 pubspec.ya

    • Firebase电话认证自动发送otp,不填写edittext。otp是在edittext中手动输入的。如何自动检测传入的验证短信并使用用户操作执行验证? Otp.kt

    • 我正在android应用程序中使用Firebase SDK进行手机身份验证OTP验证。这在调试版本上运行良好,但在发布版本上不起作用。 在Firebase项目中添加了我的调试SHA-1指纹。 在Firebase项目中添加了我的发布认证SHA-1指纹。 我还没有发布/发布构建到google play store。 提前谢谢

    • 我试图在laravel中安装jwt身份验证,但我使用Laravel5.8和jwt,如下所示https://tutsforweb.com/restful-api-in-laravel-56-using-jwt-authentication/ ,但向我显示此错误。 我的供应商/tymon/jwt auth/src/jwt.php中有任何问题吗 第182行和第200行之间 我发现了这个建议,并遵循了La