Colin Lightfoot
Colin Lightfoot

Reputation: 549

Android Phone Not Detecting MMS Until After Message is Read

I am creating an Android app in C# using Xamarin and am not understanding why my ContentObserver, MMSObserver is not noticing a change when a MMS message is received, but does notice a change after the message is read. Can a ContentObserver notice when a MMS message is received? If so, what should add/change regarding my code?

MMSObserver:

public class MMSObserver : ContentObserver
{
    private readonly Android.Net.Uri _uri;
    private static readonly string TAG = "MMS Observer";
    public static readonly string MMS_RECEIVED = "MMSObserver.intent.action.MMS_RECEIVED";

    public MMSObserver (Android.Net.Uri uri): base(null)
    {
        _uri = uri;
    }

    public override void OnChange(bool selfChange)
    {
        Log.Info(TAG, "Observed a change.");
        Task.Run(() => {
            Intent mmsIntent = new Intent(MMS_RECEIVED);
            AndroidApp.Context.SendBroadcast(mmsIntent);
        });
        base.OnChange(selfChange);
    }
}

MainActivity class:

public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
{
    // Constants for the MMS Observer.
    static readonly Android.Net.Uri MMS_URI = Android.Net.Uri.Parse("content://mms");

    // Create the MMS Observer.
    MMSObserver mmsObserver = new MMSObserver(MMS_URI);

    protected override void OnCreate(Bundle savedInstanceState)
    {
        // Other code not shown

        // Register the MMS Observer to the content resolver.
        ContentResolver.RegisterContentObserver(MMS_URI, false, mmsObserver);
        ContentResolver.NotifyChange(MMS_URI, mmsObserver);

    }

    protected override void OnDestroy()
    {
        ContentResolver.UnregisterContentObserver(mmsObserver);
        base.OnDestroy();
    }
}

Snippet of Manifest.xml:

application android:label="App.Android" android:theme="@style/MainTheme">
    <receiver android:name=".MMSReceiver">
        <!-- IntentFilterPriority.HighPriority is equal to 1000 -->
        <intent-filter android:priority="1000">
            <action android:name="android.provider.Telephony.WAP_PUSH_RECEIVED"/>
            <data android:mimeType="application/vnd.wap.mms-message" />
        </intent-filter>
    </receiver>
</application>
<uses-permission android:name="android.permission.READ_SMS"/>
<uses-permission android:name="android.permission.RECEIVE_SMS"/>
<uses-permission android:name="android.permission.RECEIVE_MMS"/>

Upvotes: 1

Views: 118

Answers (1)

Junior Jiang
Junior Jiang

Reputation: 12723

A ContentObserver only works with a ContentProvider that calls one of the NotifyChange() methods on a ContentResolver when the contents of the provider change. If the ContentProvider does not call NotifyChange(), the ContentObserver will not be notified about changes.

That means you need to call notify method manually :

ContentResolver.NotifyChange(MMS_URI, null);

Note : The second parameter can be null .

Upvotes: 2

Related Questions