Problem:
I am trying to copy the OTP code from an incoming SMS to the clipboard in a broadcast receiver. This is working fine on Galaxy A52s 5G but not on Redmi Note 9S.
Codes:
OTP Broadcast Receiver:
import ...
class OtpSmsReceiver : BroadcastReceiver() {
@SuppressLint("UnsafeProtectedBroadcastReceiver")
override fun onReceive(context: Context, intent: Intent) {
try {
val bundle = intent.extras
if (bundle != null) {
val otpSms = getNewOtpSms(bundle)
if (otpSms != null) {
val (otp, bank) = OtpSmsManager.getOtpFromSms(otpSms, true)
?: return
if (otp.isNotEmpty() && bank.isNotEmpty()) {
copyOtpToClipboard(context, otp)
Log.i("SmsReceiver", "New OTP Code from $bank: $otp")
showOtpNotification(context, otp, bank)
}
}
} // bundle is null
} catch (e: Exception) {
Log.e("SmsReceiver", "Exception smsReceiver: $e")
}
}
private fun copyOtpToClipboard(context: Context, otp: String) {
val clipboardManager =
context.getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager
clipboardManager.setPrimaryClip(
ClipData(ClipData.newPlainText("otp_code", otp))
)
}
private fun getNewOtpSms(bundle: Bundle?): OtpSms? {
if (bundle != null) {
if (bundle.containsKey("pdus")) {
val pdus = bundle.get("pdus")
if (pdus != null) {
val pdusObj = pdus as Array<*>
var message = ""
var date = ""
for (i in pdusObj.indices) {
val currentMessage = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
SmsMessage.createFromPdu(pdusObj[i] as ByteArray?, "3gpp")
} else {
SmsMessage.createFromPdu(pdusObj[i] as ByteArray?)
}
message += currentMessage.displayMessageBody
date = currentMessage.timestampMillis.toString()
} // end for loop
return OtpSms(message, date)
} else {
return null
}
} else {
return null
}
} else {
return null
}
}
}
AndroidManifest.xml:
Permissions:
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.READ_SMS" />
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS"/>
<uses-feature
android:name="android.hardware.telephony"
android:required="true" />
Broadcast Receiver Tag:
<receiver
android:name=".data.receiver.OtpSmsReceiver"
android:enabled="true"
android:exported="true"
android:permission="android.permission.RECEIVE_SMS">
<intent-filter android:priority="999">
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
Steps I have tried:
- I have checked the permissions and made sure that the broadcast receiver has the
READ_SMSandWRITE_CLIPBOARDpermissions. - I have tried restarting the device.
- I have updated the app to the latest version.
- I have checked the device logs and there are no errors related to the broadcast receiver or the clipboard.
Question:
Why is the copyOtpToClipboard() function not working on Redmi Note 9S?
Additional information:
- The device is running Android 12.
- I am using the latest version of the Android SDK and Android Studio.
I tried to using Services to solve this problem but still this peroblem was..
Expected behavior:
When an SMS is received, the broadcast receiver should copy the OTP code to the clipboard.
Actual behavior:
The broadcast receiver is receiving the SMS and extracting the OTP code, but the copyOtpToClipboard() function is not working.