I added OtpBroadcastReceiver and registred it, but I can't seem to get the messages from it

171 views Asked by At

Here is my fragment -

@SuppressLint("HardwareIds")
    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        mPresenter = VerifyOtpPresenter(this)
        androidId = Settings.Secure.getString(activity?.contentResolver, Settings.Secure.ANDROID_ID)
        mPresenter.requestOtp(phoneNumber)

        initClickAndTextListeners()
        initOtpCountdownTimer()


    }

    /**
     * Requesting OTP password for our phone number
     */
    override fun requestOtp(phoneNumber: Long) {
        OtpNetworking.requestOtp(phoneNumber, object : OtpNetworking.RequestOtpCallback {
            override fun onSuccess() {
                Toast.makeText(context, getString(R.string.verify_otp_fragment_sms_arrived), Toast.LENGTH_SHORT).show()
                startSmsRetriever()
            }

            override fun onError(reason: String) {
                Toast.makeText(context, reason, Toast.LENGTH_SHORT).show()
            }
        })
    }

    private fun startSmsRetriever() {
        val client = SmsRetriever.getClient(context!!)
        val task = client.startSmsRetriever()
        task.addOnSuccessListener {
            Toast.makeText(context, "Successfully added retriever", Toast.LENGTH_SHORT).show()
        }

        task.addOnFailureListener {
            Toast.makeText(context, "Failed to get SMS", Toast.LENGTH_SHORT).show()
        }
    }

here is my OtpBroadcastReceiver -

class OtpBroadcastReceiver : BroadcastReceiver() {
    override fun onReceive(context: Context?, intent: Intent) {
        Toast.makeText(context, "onReceive", Toast.LENGTH_SHORT).show()
        if (SmsRetriever.SMS_RETRIEVED_ACTION == intent.action) {
            val extras = intent.extras
            val status: Status? = extras!![SmsRetriever.EXTRA_STATUS] as Status?
            when (status?.statusCode) {
                CommonStatusCodes.SUCCESS -> {
                    val message: String? = extras[SmsRetriever.EXTRA_SMS_MESSAGE] as String?
                    Toast.makeText(context, message, Toast.LENGTH_SHORT).show()
                }

                CommonStatusCodes.TIMEOUT -> {
                    Toast.makeText(context, "Timeout", Toast.LENGTH_SHORT).show()
                }
            }
        }
    }
}

and my manifest file -


    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.READ_SMS"/>
    <uses-permission android:name="android.permission.RECEIVE_SMS" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE"/>

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="false"
        android:theme="@style/AppTheme">
        <activity android:name=".startup.StartupActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>


        </activity>

        <receiver android:name=".otp.service.OtpBroadcastReceiver" android:exported="true"
            android:permission="com.google.android.gms.auth.api.phone.permission.SEND">
            <intent-filter>
                <action android:name="com.google.android.gms.auth.api.phone.SMS_RETRIEVED"/>
            </intent-filter>
        </receiver>

        <meta-data
            android:name="preloaded_fonts"
            android:resource="@array/preloaded_fonts" />
    </application>

I can't seem to get any information from my broadcast receiver, eventhough the toast message of the sms retriever does say Successfully added retriever

I think I am missing the connection between the fragment and the broadcast receiver but I am not sure - does anyone have an idea what I am missing?

1

There are 1 answers

0
Jokubas Trinkunas On

You could try to pass some onOtpReceived function into your OtpBroadcastReceiver and see if that helps.

class OtpBroadcastReceiver(onOtpReceived: (String) -> Unit, onOtpTimeout: () -> Unit) : BroadcastReceiver() {
    override fun onReceive(context: Context?, intent: Intent) {

        if (SmsRetriever.SMS_RETRIEVED_ACTION == intent.action) {
            val extras = intent.extras
            val status: Status? = extras!![SmsRetriever.EXTRA_STATUS] as Status?
            when (status?.statusCode) {
                CommonStatusCodes.SUCCESS -> {
                    val message: String? = extras[SmsRetriever.EXTRA_SMS_MESSAGE] as String?
                    onOtpReceived(message)
                }

                CommonStatusCodes.TIMEOUT -> {
                   onOtpTimeout()
                }
            }
        }
    }
}