SMS BroadcastReceiver working in emulator but not physical device - Xamarin

WestMan 96 Reputation points
2020-12-03T03:15:36.683+00:00

I'm fairly new to this and I can't figure this one out after many many hours of pulling my hair out.

I stripped this down to only have the necessary code of the receiver. I can successfully deploy to the emulators and send text messages in and have it display the message in a toast message. When i build the apk and install on a physical device it doesn't seem to work. I looked at logcat and don't see the Received intent! message

 [BroadcastReceiver(Enabled = true, Exported = true, Permission = "android.permission.RECEIVE_SMS")]
    [IntentFilter(new[] { "android.provider.Telephony.SMS_RECEIVED" }, Priority = (int)IntentFilterPriority.HighPriority)]
    public class SMSReceiver : BroadcastReceiver
    {
        public override void OnReceive(Context context, Intent intent)
        {
            Toast.MakeText(context, "Received intent!", ToastLength.Short).Show();
            Log.Info("Custom App", "Received intent!");
        }
    }

Phones i've tried are Android 10

Thank you

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,283 questions
{count} votes

Accepted answer
  1. WestMan 96 Reputation points
    2020-12-04T21:34:40.457+00:00

    So it turns out it was all my fault. As you would expect. All three test phones I had, used google messages as the messaging client. Sadly they never triggers an actual SMS message when both devices are using it. It uses data in that case.

    Hopefully no one else falls into this trap. Thanks for all the help!


2 additional answers

Sort by: Most helpful
  1. Leon Lu (Shanghai Wicresoft Co,.Ltd.) 67,446 Reputation points Microsoft Vendor
    2020-12-04T07:13:04.077+00:00

    Hello,​

    Welcome to our Microsoft Q&A platform!

    Please register your SMSReceiver with explicit broadcast like following code, I test it my pixel, it worked.

    public class MainActivity : AppCompatActivity
        {
            SMSReceiver sMSReceiver;
            protected override void OnCreate(Bundle savedInstanceState)
            {
                base.OnCreate(savedInstanceState);
                Xamarin.Essentials.Platform.Init(this, savedInstanceState);
                // Set our view from the "main" layout resource
                SetContentView(Resource.Layout.activity_main);
                 sMSReceiver=   new SMSReceiver();
                var intentFilter = new IntentFilter("android.provider.Telephony.SMS_RECEIVED");
                intentFilter.Priority = 999;
                RegisterReceiver(sMSReceiver, intentFilter);
            }
    
            protected override void OnDestroy()
            {
                base.OnDestroy();
                UnregisterReceiver(sMSReceiver);
            }
    
        }
    

    And please add and grand the runtime permission.

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

    And please let your application running in the foreground is better, if you make your application in the background, if you do not use foreground service, your application will be killed.

    Best Regards,

    Leon Lu


    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our [documentation][1] to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

  2. WestMan 96 Reputation points
    2020-12-04T16:42:31.153+00:00

    I tried on my Pixel 3a with no luck. I feel like there's a setting i'm missing. When i have the app open i put a break point on the Toast message in the Receiver and it never gets hit when an SMS comes in.

    using Android.App;  
    using Android.Content;  
    using Android.Widget;  
    using System;  
      
    namespace ImWatching  
    {  
        [BroadcastReceiver(Enabled = true, Exported = true, Permission = "android.permission.RECEIVE_SMS")]  
        [IntentFilter(new[] { "android.provider.Telephony.SMS_RECEIVED" }, Priority = (int)IntentFilterPriority.HighPriority)]  
        public class SMSReceiver : BroadcastReceiver  
        {  
            public override void OnReceive(Context context, Intent intent)  
            {  
                Toast.MakeText(context, "Received intent!", ToastLength.Short).Show();  
            }  
        }  
    }  
    
    
    using Android;  
    using Android.App;  
    using Android.Content;  
    using Android.OS;  
    using Android.Runtime;  
    using Android.Support.V4.App;  
    using Android.Support.V7.App;  
      
    namespace ImWatching  
    {  
        [Activity(Label = "@string/app_name", Theme = "@style/AppTheme.NoActionBar", MainLauncher = true)]  
        public class MainActivity : AppCompatActivity  
        {  
            SMSReceiver sMSReceiver;  
            protected override void OnCreate(Bundle savedInstanceState)  
            {  
                base.OnCreate(savedInstanceState);  
                Xamarin.Essentials.Platform.Init(this, savedInstanceState);  
                SetContentView(Resource.Layout.activity_main);  
      
                var permissions = new string[]  
               {  
                    Manifest.Permission.ReceiveSms,  
                    Manifest.Permission.ReadSms,  
                    Manifest.Permission.BroadcastSms,  
                    Manifest.Permission.WriteSms,  
                    Manifest.Permission.SendSms  
               };  
                ActivityCompat.RequestPermissions(this, permissions, 123);  
      
                sMSReceiver = new SMSReceiver();  
      
                var intentFilter = new IntentFilter("android.provider.Telephony.SMS_RECEIVED");  
                intentFilter.Priority = 999;  
                RegisterReceiver(sMSReceiver, intentFilter);  
      
      
                Android.Support.V7.Widget.Toolbar toolbar = FindViewById<Android.Support.V7.Widget.Toolbar>(Resource.Id.toolbar);  
                SetSupportActionBar(toolbar);             
                
            }  
           
      
            protected override void OnDestroy()  
            {  
                base.OnDestroy();  
                UnregisterReceiver(sMSReceiver);  
            }  
      
            public override void OnRequestPermissionsResult(int requestCode, string[] permissions, [GeneratedEnum] Android.Content.PM.Permission[] grantResults)  
            {  
                Xamarin.Essentials.Platform.OnRequestPermissionsResult(requestCode, permissions, grantResults);  
      
                base.OnRequestPermissionsResult(requestCode, permissions, grantResults);              
              
            }  
        }  
    }  
    
    
    <?xml version="1.0" encoding="utf-8"?>  
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"   
              android:versionCode="1"   
              android:versionName="1.0"   
              package="com.companyname.imwatching">  
      <uses-sdk android:minSdkVersion="24" android:targetSdkVersion="28" />  
      <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/AppTheme">  
      </application>  
    	<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />  
    	<uses-permission android:name="android.permission.BROADCAST_SMS" />  
    	<uses-permission android:name="android.permission.READ_SMS" />  
    	<uses-permission android:name="android.permission.RECEIVE_SMS" />  
    	<uses-permission android:name="android.permission.SEND_SMS" />  
    	<uses-permission android:name="android.permission.WRITE_SMS" />  
    </manifest>  
    
    
    
    
    
    
      
    
    0 comments No comments