OiO.lk Blog Android Recaptcha fails to initialize on first launch of the Android App
Android

Recaptcha fails to initialize on first launch of the Android App


I’m developing an Android application that integrates Google reCAPTCHA (v3) for user registration. I’ve encountered a persistent issue where the reCAPTCHA client fails to initialize on the first launch of the app for certain devices, resulting in the error message: "ReCAPTCHA not initialized. Please try again." and the initializing fails because "Invalid site Key", the key is valid and it works when you close the app and open it again. here is my code and how I’m implementing it.

class ReCaptchaProjectClient(private val coroutineScope: CoroutineScope, private val application: Application) {

    private lateinit var recaptchaClient: RecaptchaClient

    init {
        initializeRecaptchaClient()
    }

    private fun initializeRecaptchaClient() {
        coroutineScope.launch {
            try {
                recaptchaClient = Recaptcha.fetchClient(
                    application,
                    "recaptchaSiteKey"
                )
            } catch (e: Exception) {
                Log.e("ReCaptcha123", "Error initializing RecaptchaClient: $e")
            }
        }
    }

    fun executeRegisterAction(onTokenReceived: (String?) -> Unit) {
        coroutineScope.launch {
            try {
                if (!::recaptchaClient.isInitialized) {
                    onTokenReceived(null)
                    return@launch
                }

                val token = withContext(Dispatchers.IO) {
                    recaptchaClient.execute(RecaptchaAction.SIGNUP)
                }
                onTokenReceived(token.toString().removePrefix("Success(").removeSuffix(")"))
            } catch (e: Exception) {
                onTokenReceived(null)
            }
        }
    }



}

and I’m initializing it in my application class:

class PROJECTApp : Application(), CoroutineScope {

    lateinit var recaptchaClient: RecaptchaClient
    lateinit var reCaptchaProjectClient: ReCaptchaProjectClient
    private val job = Job()

    override val coroutineContext: CoroutineContext
        get() = Dispatchers.Main + job

    override fun onCreate() {
        super.onCreate()
        FirebaseApp.initializeApp(this)

        reCaptchaProjectClient = ReCaptchaProjectClient(this, this)
        initRecaptchaClient()
    }

    private fun initRecaptchaClient() {
        launch {
            try {
                recaptchaClient = Recaptcha.fetchClient(this@PROJECTApp, "sitekey")
            } catch (e: Exception) {
                e.printStackTrace()
            }
        }
    }

    override fun onTerminate() {
        super.onTerminate()
        job.cancel()
    }
}```




I have tried retry mechanism, ensuring the site key is correct moving the initializing to the activity and still the only way the problem goes away is to close the app and open it again, i cant find anyone else with the same issue, is there something im doing wrong?



You need to sign in to view this answers

Exit mobile version