Forum Xamarin Xamarin.Forms

Read and receive SMS From Devices

xamnetxamnet INMember ✭✭

Hi All,

I want to read and receive SMS from my devices using Xamarin.Forms(Android & iOS)

Is there any one have an idea how to I implement?

Please guide me.

Answers

  • AndrewMcCormackAndrewMcCormack USUniversity ✭✭✭

    https://github.com/cjlotz/Xamarin.Plugins

    Android read SMS:
    http://stackoverflow.com/questions/848728/how-can-i-read-sms-messages-from-the-device-programmatically-in-android
    http://stackoverflow.com/questions/7089313/android-listen-for-incoming-sms-messages

    // public static final String INBOX = "content://sms/inbox";
    // public static final String SENT = "content://sms/sent";
    // public static final String DRAFT = "content://sms/draft";
    Cursor cursor = getContentResolver().query(Uri.parse("content://sms/inbox"), null, null, null, null);

    if (cursor.moveToFirst()) { // must check the result to prevent exception
    do {
    String msgData = "";
    for(int idx=0;idx<cursor.getColumnCount();idx++)
    {
    msgData += " " + cursor.getColumnName(idx) + ":" + cursor.getString(idx);
    }
    // use msgData
    } while (cursor.moveToNext());
    } else {
    // empty box, no SMS
    }

    iOS:
    Send but not read.

  • BrentAriasBrentArias USMember ✭✭

    Will this approach work if I'm trying to extract a v-card from SMS?

  • edslasedslas Member

    You need to request permissions in manifest to receive sms:
    <uses-permission android:name="android.permission.RECEIVE_SMS"/>

    Write a broadcast receiver:

    using Android.App;
    using Android.Content;
    using Android.OS;
    using Android.Provider;
    using Android.Telephony;
    using Android.Util;
    using System;
    
    namespace ActivityAlert.Droid
    {
        [BroadcastReceiver(Enabled = true, Exported = true)]
        [IntentFilter(new[] { "android.provider.Telephony.SMS_RECEIVED" })]
        class SmsReceiver : BroadcastReceiver
        {
            private const string TAG = "AA:SmsReceiver";
            public override void OnReceive(Context context, Intent intent)
            {
    
                if (intent.Action.Equals(Telephony.Sms.Intents.SmsReceivedAction))
                {
                    var msgs = Telephony.Sms.Intents.GetMessagesFromIntent(intent);
                    foreach( var msg in msgs)
                    {
                        Log.Debug(TAG, $" MessageBody {msg.MessageBody}");
                        Log.Debug(TAG, $"DisplayOriginatingAddress {msg.DisplayOriginatingAddress}");
                        Log.Debug(TAG, $"OriginatingAddress {msg.OriginatingAddress}");
                    }
                }
    
            }
    
        }
    }
    
  • mubimubi USMember ✭✭✭

    @AndrewMcCormack said:
    https://github.com/cjlotz/Xamarin.Plugins

    Android read SMS:
    http://stackoverflow.com/questions/848728/how-can-i-read-sms-messages-from-the-device-programmatically-in-android
    http://stackoverflow.com/questions/7089313/android-listen-for-incoming-sms-messages

    // public static final String INBOX = "content://sms/inbox";
    // public static final String SENT = "content://sms/sent";
    // public static final String DRAFT = "content://sms/draft";
    Cursor cursor = getContentResolver().query(Uri.parse("content://sms/inbox"), null, null, null, null);

    if (cursor.moveToFirst()) { // must check the result to prevent exception
    do {
    String msgData = "";
    for(int idx=0;idx<cursor.getColumnCount();idx++)
    {
    msgData += " " + cursor.getColumnName(idx) + ":" + cursor.getString(idx);
    }
    // use msgData
    } while (cursor.moveToNext());
    } else {
    // empty box, no SMS
    }

    iOS:
    Send but not read.

    Can I have the Xamarin Version of this Code?

Sign In or Register to comment.