我尝试使用Firebase实施电话身份验证,但没有收到任何短信

时间:2019-06-09 18:41:09

标签: android firebase kotlin firebase-authentication

我正在尝试使用电话号码实现firebase auth方法,但是当我单击该按钮以接收我的短信代码时,进度栏只会加载,而我却没有收到任何短信。在Firebase控制台上也不会创建任何帐户,因为如果我不输入验证码就无法注册帐户。

RegisterLoginActivity

package com.example.madistrezsieu

import android.content.Intent

import android.os.Bundle
import android.view.View
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
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 kotlinx.android.synthetic.main.activity_register_login.*
import java.util.concurrent.TimeUnit


class RegisterLoginActivity : AppCompatActivity() {

    lateinit var mCallbacks: PhoneAuthProvider.OnVerificationStateChangedCallbacks
    lateinit var mAuth: FirebaseAuth
    var verificationId = ""


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_register_login)
        mAuth = FirebaseAuth.getInstance()
        veriBtn.setOnClickListener {
                view: View? -> progress.visibility = View.VISIBLE
            verify ()
        }
        authBtn.setOnClickListener {
                view: View? -> progress.visibility = View.VISIBLE
            authenticate()
        }
    }


    private fun verificationCallbacks () {
        mCallbacks = object: PhoneAuthProvider.OnVerificationStateChangedCallbacks() {
            override fun onVerificationCompleted(credential: PhoneAuthCredential) {
                progress.visibility = View.INVISIBLE
                signIn(credential)
            }

            override fun onVerificationFailed(p0: FirebaseException?) {
                //TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
            }

            override fun onCodeSent(verfication: String?, p1: PhoneAuthProvider.ForceResendingToken?) {
                super.onCodeSent(verfication, p1)
                verificationId = verfication.toString()
                progress.visibility = View.INVISIBLE
            }

        }
    }

    private fun verify () {

        verificationCallbacks()

        val phnNo = phnNoTxt.text.toString()

        PhoneAuthProvider.getInstance().verifyPhoneNumber(
            phnNo,
            60,
            TimeUnit.SECONDS,
            this,
            mCallbacks
        )
    }

    private fun signIn (credential: PhoneAuthCredential) {
        mAuth.signInWithCredential(credential)
            .addOnCompleteListener {
                    task: Task<AuthResult> ->
                if (task.isSuccessful) {
                    toast("Logged in Successfully :)")
                    startActivity(Intent(this, LatestMessagesActivity::class.java))
                }
            }
    }

    private fun authenticate () {

        val verifiNo = verifiTxt.text.toString()

        val credential: PhoneAuthCredential = PhoneAuthProvider.getCredential(verificationId, verifiNo)

        signIn(credential)

    }

    private fun toast (msg: String) {
        Toast.makeText(this, msg, Toast.LENGTH_LONG).show()
    }


}

activity_register_login.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                xmlns:tools="http://schemas.android.com/tools"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                tools:context="com.example.madistrezsieu.RegisterLoginActivity">

    <EditText
            android:id="@+id/phnNoTxt"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="88dp"
            android:hint="Phone Number"
            android:inputType="number"
            android:textAlignment="center"
            android:textSize="24sp" />

    <EditText
            android:id="@+id/verifiTxt"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentStart="true"
            android:textAlignment="center"
            android:inputType="number"
            android:layout_below="@+id/phnNoTxt"
            android:layout_marginTop="43dp"
            android:hint="Verification Code" android:layout_alignParentLeft="true"/>

    <Button
            android:id="@+id/veriBtn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/verifiTxt"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="35dp"
            android:text="Verify" />

    <Button
            android:id="@+id/authBtn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/veriBtn"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="26dp"
            android:text="Authenticate" />

    <ProgressBar
            android:id="@+id/progress"
            style="?android:attr/progressBarStyle"
            android:layout_width="150dp"
            android:layout_height="150dp"
            android:layout_alignTop="@+id/phnNoTxt"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="28dp"
            android:visibility="invisible" />
</RelativeLayout>

1 个答案:

答案 0 :(得分:0)

  1. 确保您在Firebase控制台上启用了电话身份验证,
  2. 请确保您在Android Studio上的项目已连接到Firebase,因为电话身份验证需要SHA-1证书才能发送短信。您的android studio将自动处理SHA-1。
  3. 有时候通过android studio将firebase连接到android项目时会出现问题,在这种情况下,您应该为项目手动创建自己的SHA-1。按照此链接进行操作SHA-1 fingerprint of keystore certificate