Android BroadcastReceiver Not Receiving SMS Messages
I’m developing an Android app that should automatically respond to incoming SMS messages. The app works fine initially on a few devices but started to fail to receive SMS messages on two , including my Pixel 3a running Android 12 and my Pixel 4a running Android 13. Sending SMS messages works perfectly, but receiving doesn’t trigger the BroadcastReceiver
any more.
I’ve ensured that the necessary permissions are granted, and the BroadcastReceiver
is correctly registered in the AndroidManifest.xml
. Below is the relevant code and manifest configuration.
AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.myapp">
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.READ_SMS" />
<uses-permission android:name="android.permission.SEND_SMS" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.MyApp">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver
android:name=".SmsReceiver"
android:exported="true">
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
</application>
</manifest>
Here is the SmsReceiver.kt
package com.example.myapp
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.telephony.SmsMessage
import android.util.Log
class SmsReceiver : BroadcastReceiver() {
override fun onReceive(context: Context?, intent: Intent?) {
Log.d("SmsReceiver", "onReceive called")
if (intent?.action == "android.provider.Telephony.SMS_RECEIVED") {
val bundle: Bundle? = intent.extras
if (bundle != null) {
try {
val pdus = bundle["pdus"] as Array<*>
for (pdu in pdus) {
val smsMessage: SmsMessage = SmsMessage.createFromPdu(pdu as ByteArray)
val sender = smsMessage.originatingAddress
val messageBody = smsMessage.messageBody
Log.d("SmsReceiver", "Received SMS from $sender: $messageBody")
// Additional logic here
}
} catch (e: Exception) {
Log.e("SmsReceiver", "Exception: $e")
}
}
}
}
}
What could be preventing the BroadcastReceiver from receiving SMS messages on some devices? Are there any device-specific settings or configurations that I need to check? Any help or pointers would be greatly appreciated!
Gone to Settings
-> Apps
-> MyApp
-> Permissions
,
And confirmed that SMS permissions were set to Always