首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Firebase PhoneAuthProvider动态口令

Firebase PhoneAuthProvider动态口令
EN

Stack Overflow用户
提问于 2018-03-01 01:42:32
回答 2查看 1K关注 0票数 2

我已经做了一个简单的聊天应用程序,我正在尝试添加使用Firebase电话认证的电话号码验证。我已经成功地添加了所有依赖项,数据库运行良好。但是当我点击send OTP按钮时,我没有通过消息得到任何代码。

这是完整的类。

代码语言:javascript
复制
package com.sharathnewdev.flashchatnewfirebase;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.FirebaseException;
import com.google.firebase.auth.AuthResult;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.PhoneAuthCredential;
import com.google.firebase.auth.PhoneAuthProvider;

import java.util.concurrent.TimeUnit;

public class FB_Phone extends AppCompatActivity {
    FirebaseAuth mAuth;
    EditText et1, et2;
    Button sendbutton, verifybutton;
    PhoneAuthProvider.OnVerificationStateChangedCallbacks mCallbacks;
    String Verificationcode;
    private static final String TAG = "FB_Phone";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fb_phone);
        et1 = findViewById(R.id.mobileNUMBERTV);
        et2 = findViewById(R.id.verifyTV);
        sendbutton = findViewById(R.id.sendbutton);
        verifybutton = findViewById(R.id.verifybutton);
        sendbutton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                send_Sms();
            }
        });
        verifybutton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                verify();
            }
        });
        mAuth = FirebaseAuth.getInstance();
        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);
                Verificationcode = s;
                Toast.makeText(getApplicationContext(), Verificationcode, Toast.LENGTH_SHORT).show();
                Toast.makeText(getApplicationContext(), "The code has been sent to the user", Toast.LENGTH_SHORT).show();
            }
        };

    }

    public void send_Sms() {
        String sent_code = et1.getText().toString();
        Toast.makeText(getApplicationContext(), sent_code, Toast.LENGTH_SHORT).show();
        PhoneAuthProvider.getInstance().verifyPhoneNumber(sent_code, 60, TimeUnit.SECONDS, this, mCallbacks);
    }

    public void signInWithPhone(PhoneAuthCredential credential) {
        mAuth.signInWithCredential(credential).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
            @Override
            public void onComplete(@NonNull Task<AuthResult> task) {
                if (task.isSuccessful()) {
                    Toast.makeText(getApplicationContext(), "The use has been successfully signed in", Toast.LENGTH_SHORT).show();
                    Intent chatIntent = new Intent(FB_Phone.this, MainChatActivity.class);
                    startActivity(chatIntent);
                }
            }
        });
    }

    public void verify() {
        String recieved_code = et2.getText().toString();
        VerifywithPhone(Verificationcode, recieved_code);

    }

    public void VerifywithPhone(String verificationcode, String recievedcode) {
        PhoneAuthCredential credential = PhoneAuthProvider.getCredential(verificationcode, recievedcode);
        signInWithPhone(credential);

    }
}

在这里,当按下send按钮时,我调用了send_sms方法。但它不会通过短信给我发任何东西。我尝试对其进行调试,但该过程只是运行,没有任何反应

Logcat显示此错误

java.lang.IllegalArgumentException:没有verificationProof、sessionInfo或临时证明,无法创建PhoneAuthCredential。

请帮帮忙。我到底做错了什么?

EN

回答 2

Stack Overflow用户

发布于 2018-03-02 20:41:14

感谢大家的回复。我最终找到了问题所在。

问题是我还没有将我的应用程序SHA-1指纹添加到firebase控制台中。

直到今天,我还没有检查onVerificationFailed方法是否有任何潜在的错误。

我添加了在OnverificationFailed方法上显示toast消息的代码。

我清楚地知道了错误,然后使用this指南将SHA-1指纹添加到控制台。

我太傻了,不用它。

再次感谢。

票数 1
EN

Stack Overflow用户

发布于 2018-03-01 22:07:09

这非常简单,首先启动模拟器并启动应用程序。现在,如果您有另一个项目(任何项目),请在另一个模拟器中启动此项目。在另一个模拟器上,启动短信应用程序,并发送短信到模拟器,其中您的应用程序是running.The phone no。是仿真器的简单编号,如5556或5554。

立即编辑您可以使用Emulator控件发送短信或拨打电话。在Eclipse中,转到window->show views->other->Emulator control。

https://stackoverflow.com/a/7638361/3980203

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49035583

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档