首页 > 解决方案 > 未收到来自 Firebase Phone Auth(印度)的 OTP

问题描述

我正在创建一个使用来自 Firebase Phone Auth 的登录的 android 应用程序。我以前使用的是电子邮件验证,这就像魅力一样,现在我正在同一个项目上迁移到 Phone Auth。我在片段上写我的身份验证,当我关注给定的链接https://firebase.google.com/docs/auth/android/phone-auth时,我收到编译错误说“以下函数都不能调用提供参数” ON “PhoneAuthProvider.getInstance().verifyPhoneNumber”,以下是代码,

毕业典礼


    apply plugin: 'com.android.application'

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

    apply plugin: 'kotlin-android'

    apply plugin: 'kotlin-android-extensions'

    apply plugin: 'kotlin-kapt'

    apply plugin: "androidx.navigation.safeargs.kotlin"


    android {
        compileSdkVersion 29
        buildToolsVersion "29.0.2"
        defaultConfig {
            applicationId "com.sstech.racemanager"
            minSdkVersion 22
            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'
            }
        }
        // Enables data binding.
        dataBinding {
            enabled = true
        }

    }

    dependencies {
        // def versions
        def nav_version = "2.1.0"
        def lifecycle_version = "2.1.0"

    //    kapt("groupId:artifactId:version")
        implementation fileTree(dir: 'libs', include: ['*.jar'])
        implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
        implementation 'androidx.appcompat:appcompat:1.1.0'
        implementation 'androidx.core:core-ktx:1.1.0'
        implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
        implementation 'androidx.legacy:legacy-support-v4:1.0.0'
        testImplementation 'junit:junit:4.12'
        androidTestImplementation 'androidx.test.ext:junit:1.1.1'
        androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
        // Add the Firebase SDK for Google Analytics
        implementation 'com.google.firebase:firebase-analytics:17.2.1'

        // Add the SDKs for any other Firebase products you want to use in your app
        // For example, to use Firebase Authentication and Cloud Firestore
        implementation 'com.google.firebase:firebase-auth:19.2.0'
        implementation 'com.google.firebase:firebase-firestore:21.3.1'
        implementation 'com.google.firebase:firebase-storage:19.1.0'
        implementation 'com.google.firebase:firebase-database:19.2.0'

        // Fragment
        implementation "androidx.navigation:navigation-fragment-ktx:$nav_version"
        implementation "androidx.navigation:navigation-ui-ktx:$nav_version"

        // LifeCycle
        implementation "androidx.lifecycle:lifecycle-extensions:$lifecycle_version"
        kapt "androidx.lifecycle:lifecycle-compiler:$lifecycle_version"

        // Circle Image View
        implementation 'de.hdodenhof:circleimageview:3.0.1'
    }

片段类


    class RegisterPage : Fragment() {
        lateinit var binding: FragmentRegisterBinding
        lateinit var callbacks: PhoneAuthProvider.OnVerificationStateChangedCallbacks
        lateinit var mAuth: FirebaseAuth
        lateinit var otpPin: String
        override fun onCreateView(
            inflater: LayoutInflater,
            container: ViewGroup?,
            savedInstanceState: Bundle?): View? {
            mAuth= FirebaseAuth.getInstance()

            binding = DataBindingUtil.inflate(inflater,
                R.layout.fragment_register,container,false)

            binding.loginAlready.setOnClickListener { view: View->
                view.findNavController().navigate(R.id.action_registerPage_to_logPage)
            }

            binding.register.setOnClickListener {
                    registerAUser(binding)

            }

            binding.loginImage.setOnClickListener{
                    selectimage()

            }

    //        view.findNavController().navigate(R.id.action_registerPage_to_logPage)


            return binding.root
        }

        override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
            super.onViewCreated(view, savedInstanceState)
        }

        private fun selectimage(){
    //        val intent = Intent(Intent.ACTION_PICK_ACTIVITY)
    ////        intent.type="image/*"
            val intent = Intent(Intent.ACTION_GET_CONTENT)
            intent.type="image/*"
            startActivityForResult(Intent.createChooser(intent,"pick an image"),0)

        }
            var selectedImageUri : Uri?= null

        override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
            super.onActivityResult(requestCode, resultCode, data)
            Toast.makeText(activity,"entered result",Toast.LENGTH_SHORT).show()
            if(requestCode==0 && resultCode== Activity.RESULT_OK && data != null){
                // image ok
                try {
                     selectedImageUri = data.data
                    val bitmap: Bitmap =
                        MediaStore.Images.Media.getBitmap(activity?.contentResolver, selectedImageUri)
                        binding.loginImageRound.setImageBitmap(bitmap)
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
                        binding.loginImage.alpha=0f
                    }else{
                        binding.loginImage.visibility=INVISIBLE
                    }
    //                val bitmapDrawable = BitmapDrawable(bitmap)
    //                binding.loginImage.setBackgroundDrawable(bitmapDrawable)
                }catch(ex:Exception){
                    Toast.makeText(activity, ex.localizedMessage,Toast.LENGTH_SHORT).show()
                }


            }else{
                if(requestCode==0){Toast.makeText(activity,"resultCode ok",Toast.LENGTH_LONG).show()}
                if(resultCode== Activity.RESULT_OK){Toast.makeText(activity,"resultCode error",Toast.LENGTH_LONG).show()}

                if(data==null){
                    Toast.makeText(activity,"Data is empty",Toast.LENGTH_LONG).show()
                }

            }
        }


       private fun registerAUser(binding: FragmentRegisterBinding){
           val phno : String = binding.loginPhno.text.toString()
           val name : String = binding.loginName.text.toString()
            if(phno.isEmpty() || name.isEmpty() ){
                Toast.makeText(activity,"Phone number/Name should not be empty",Toast.LENGTH_SHORT).show()
                return
            }
           if(phno.length!=10) {
               Toast.makeText(activity,"Phone number should be 10 numbers",Toast.LENGTH_SHORT).show()
               return
           }

           verify("+91$phno")

       }

        private fun verify(phno: String) {
            verificationCallbacks()
            Log.i("ph",phno)
            PhoneAuthProvider.getInstance().verifyPhoneNumber(
                phno, // Phone number to verify
                60, // Timeout duration
                TimeUnit.SECONDS, // Unit of timeout
                this, // Activity (for callback binding)
                callbacks) // OnVerificationStateChangedCallbacks
            Log.i("ph","Code Sent")
        }


        private fun verificationCallbacks (){
            callbacks= object : PhoneAuthProvider.OnVerificationStateChangedCallbacks(){
                override fun onCodeSent(p0: String, p1: PhoneAuthProvider.ForceResendingToken) {
                    super.onCodeSent(p0, p1)
                    Log.i("ph","On Code Sent Cred $p0")

                }

                override fun onCodeAutoRetrievalTimeOut(p0: String) {
                    super.onCodeAutoRetrievalTimeOut(p0)

                }

                override fun onVerificationCompleted(p0: PhoneAuthCredential) {
                    Log.i("ph","Sign in verification callback")
                   signInWithPhoneAuthCredential(p0)
                }

                override fun onVerificationFailed(p0: FirebaseException) {
                    if (p0 is FirebaseAuthInvalidCredentialsException) {
                        Toast.makeText(activity,"Invalid request",Toast.LENGTH_SHORT).show()
                    } else if (p0 is FirebaseTooManyRequestsException) {
                        Toast.makeText(activity,"Quota Over contact Developer",Toast.LENGTH_SHORT).show()
                    }
                }
            }

        }

        private fun signInWithPhoneAuthCredential(credential: PhoneAuthCredential){
            mAuth.signInWithCredential(credential)
                .addOnCompleteListener { task: Task<AuthResult> ->
                    if (task.isSuccessful) {
                        // Sign in success, update UI with the signed-in user's information

                        val user = task.result?.user
                        // ...
                    } else {
                        // Sign in failed, display a message and update the UI
                        if (task.exception is FirebaseAuthInvalidCredentialsException) {
                            // The verification code entered was invalid
                        }
                    }
                }

        }


    }

在跟踪和错误之后,我将 MainActivity() 放在“活动”位置后,我的错误消失了,但是如果代码没有进入 OnCodeSent 函数,我没有收到 OTP。(我在控制台上启用了电话身份验证)

有人可以在这里帮忙。

标签: androidkotlinfirebase-authentication

解决方案


当我将身份验证系统从邮件更改为电话号码时,我忘记将我的应用程序重新连接到 firebase。

重新连接后一切正常


推荐阅读