Using the SMS MessageInterception Managed Classes

Following up on my "why" post to using the new managed SMS Interception namespace with Windows Mobile 5.0 devices, I thought it might be a good idea to demonstrate the ease of implementing this type of functionality. In other words, show "how" in addition to "why" ;-)

To use the SMS message interception, perform the following steps -

  • In your Smart Device project, add references to to Microsoft.WindowsMobile, Microsoft.WindowsMobile.PocketOutlook and Microsoft.WindowsMobile.PocketOutlook.MessageInterception. It would also be a good idea to add these to your code with using statements.

    using Microsoft.WindowsMobile;
    using Microsoft.WindowsMobile.PocketOutlook;
    using Microsoft.WindowsMobile.PocketOutlook.MessageInterception;

  • There are two major objects you will be working with here. The MessageInterceptor (responsible for actual interception) and the MessageCondition (and object that sets up the criteria for messages to intercept). You will need to create instances of these two objects in the containing class.

    // For SMS Message Interception...
    private MessageInterceptor _SMSCatcher = new MessageInterceptor(InterceptionAction.NotifyAndDelete, true);
    // For setting up the message filter...
    private MessageCondition _SMSFilter = new MessageCondition();

    The constructor for MessageInterceptor has two significant arguments. The first allows you to define an interception action (what to do when a message is intercepted). Typically, these messages do not need to been directly seen or stored, so the NotifiyAndDelete enumeration of the InterceptionAction enumerator works well. The second argument sets whether or not to use the current form's thread. Usually, this is set to true, but if your application does not have a form, it would be set to false.

  • You will want to set up an event handler and event method for taking action when the event occurs. To do this, you will set up an event handler for the MessageReceived event of the MessageInterceptor. The standard Visual Studio functionality (using TABs to create the event handler and associated method) work here.

    //SMS Message Interception event handler...
    _SMSCatcher.MessageReceived += new MessageInterceptorEventHandler(_SMSCatcher_MessageReceived);

  • You will need to set up the MessageCondition object with the necessary filters/criteria and assign it to the MessageInterceptor.

    //SMS Message Filter setup...
    _SMSFilter.Property = MessageProperty.Body;
    _SMSFilter.ComparisonType =
    MessagePropertyComparisonType.StartsWith;
    _SMSFilter.CaseSensitive =
    true;
    _SMSFilter.ComparisonValue =
    "OutlookMobileDemo";

    //Assign the filter to the Interceptor...
    _SMSCatcher.MessageCondition = _SMSFilter;

    The Property property (redudant redundacy intended) is set to one of several values that represent what part of the message will be queried for the filter; the body of the message, the message class (technically, you can use transports other than SMS), the sender or the subject. For SMS, the Body or Sender enumerations are most commonly used. The ComparisonType property is used to describe what kind of comparison should be made (StartsWith, EndsWidth, Contains, Equal or NotEqual) to the Property selected. This is really up to you and your application needs. The CaseSensitive property can be used to further refine the selection criteria, and the ComparisonValue property is the actual text to use for comparison.

  •  Now, all you have left to do is to write your event code! I think it's important to note that the MessageInterceptorEventArgs object passed to the event handler contains the entire SMS message in it's Message property. By casting this property to an SmsMessage object, you can query all of the SMS message's information. As an example, I commonly demo the process in action by displaying a notification message (using the Notification control) to the user -

    void _SMSCatcher_MessageReceived(object sender, MessageInterceptorEventArgs e)
    {
       
    //Event args pass the SMS message...
       
    SmsMessage mySMS = (SmsMessage)e.Message;
       
    //Show a notification popup...
       
    this.notifyCustomIncoming.Caption = "New SMS";
       
    this.notifyCustomIncoming.InitialDuration = 60;
       
    this.notifyCustomIncoming.Text = "Got the special SMS! - " + mySMS.Body.ToString();
       
    this.notifyCustomIncoming.Visible = true;
    }

Published Mon, Apr 3 2006 20:05 by Dons

Comments

# MessageInterception API Sample

Tuesday, April 04, 2006 1:43 PM by DonS.CF
What better way to follow up to my post on the Compact Framework / Windows Mobile 5.0 MessageInterception...

Leave a Comment

(required) 
(required) 
(optional)
(required)