【问题标题】:Firebase PhoneAuthProvider OTPFirebase PhoneAuthProvider OTP
【发布时间】:2018-08-08 16:31:21
【问题描述】:

我制作了一个简单的聊天应用程序,我正在尝试使用 Firebases 电话身份验证添加电话号码验证。我已成功添加所有依赖项,并且数据库工作正常。但是当我点击发送 OTP 按钮时,我没有通过消息收到任何代码。

这是完整的课程。

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_sms 方法。但它只是不通过短信向我发送任何内容。我尝试调试它,但进程只是运行,没有任何反应

Logcat 显示此错误

java.lang.IllegalArgumentException:在没有验证证明、会话信息或临时证明的情况下无法创建 PhoneAuthCredential。

请帮忙。我在这里做错了什么?

【问题讨论】:

  • 您在什么设备上测试应用程序?
  • 您无法在模拟器上测试 OTP。
  • 大家好,很抱歉延迟回复。我正在 Galaxy j5 2015 型号上对其进行测试。短信功能在此手机上运行良好,短信权限也已启用。

标签: android firebase authentication firebase-authentication


【解决方案1】:

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

这是因为我没有将我的应用程序 SHA-1 指纹添加到 Firebase 控制台。

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

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

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

不使用它真是太愚蠢了。

再次感谢。

【讨论】:

    【解决方案2】:

    首先启动模拟器并启动您的应用程序非常简单。现在,如果您有另一个项目(任何),请在另一个模拟器中启动它。在另一个模拟器上启动 SMS 应用程序并向运行您的应用程序的模拟器发送短信。电话号码。只是模拟器编号,如 5556 或 5554。

    编辑 现在您可以使用 Emulator Control 发送 SMS 或拨打电话。在 Eclipse 中转到 window->show views->other->Emulator control.

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-11-18
      • 2019-04-12
      • 1970-01-01
      • 2022-12-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多