I have successfully implemented SMS retriever in my application using the guideline here . My Code is working fine and otp is auto populated in many devices but some of the devices like vivo v15 pro, redmi note 4 it is not working (BroadcastReceiver's onReceive() not get triggered) . i have attached my code here. check and let me know if you have any solution for this. Thanks
Manifest.xml
<receiver android:name=".sms.SMSRetrieverBroadcastReceiver" android:enabled="true" android:exported="true"> <intent-filter> <action android:name="com.google.android.gms.auth.api.phone.SMS_RETRIEVED" /> </intent-filter> </receiver>
SMSRetrieverBroadcastReceiver.kt
class SMSRetrieverBroadcastReceiver : BroadcastReceiver() { companion object{ private var otpReceiver: OtpReceiver? = null fun initOTPListener(receiver: OtpReceiver) { this.otpReceiver = receiver } } override fun onReceive(context: Context, intent: Intent) { if (SmsRetriever.SMS_RETRIEVED_ACTION == intent.action) { val extras = intent.extras val status = extras!!.get(SmsRetriever.EXTRA_STATUS) as Status when (status.statusCode) { CommonStatusCodes.SUCCESS -> { // Get SMS message contents var otp: String = extras.get(SmsRetriever.EXTRA_SMS_MESSAGE) as String Timber.d("OTP_Message "+otp) if(otpReceiver != null) { otpReceiver!!.onOTPReceived(otp) } } CommonStatusCodes.TIMEOUT -> { // Waiting for SMS timed out (5 minutes) // Handle the error ... if(otpReceiver != null) { otpReceiver!!.onOTPTimeOut() } } } }}
MainActivity.kt
fun startListeningForSMS() { SMSRetrieverBroadcastReceiver.initOTPListener(this) startSmsListener() } private fun startSmsListener() { val client = SmsRetriever.getClient(mContext) val task = client.startSmsRetriever() task.addOnSuccessListener { Timber.d("Success") } task.addOnFailureListener { Timber.d("Failed") } } override fun onOTPReceived(otp: String) { setReceivedOtp(otp) } override fun onOTPTimeOut() { setReceivedOtp(null) }
Go to Messeges-click on (:) on top left ->Go to setting->Disable prevention from Otp verification.It worked fine in my case.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With