Intercept SMS messages

Screenshot of SMS Device Control sample applicationMany TV game shows use SMS text messages for viewer voting and other forms of participation. This blog post discusses how a Windows Mobile application can automatically respond to incoming SMS text messages to create such services.

Creating a Message Interceptor

The first step in catching received SMS text messages is to create an instance of the MessageInterceptor class that lives in the Microsoft.WindowsMobile.PocketOutlook assembly. You need to be careful about where you define this instance as if it goes out of scope and is garbage collected the message interception will stop.

MessageInterceptor interceptor =
  new MessageInterceptor(InterceptionAction.NotifyAndDelete);

An InterceptionAction is passed to the constructor. This parameter defines the behaviour which occurs when a message is received. The two options are:

  • Notify – The message interceptor gets a chance to process the message but it is also received by the standard SMS inbox application.
  • NotifyAndDelete – The message does not become visible to the user and is only seen by the message interceptor.

Specifying an interception condition

A message interceptor that processed and potentially deleted every message would not be very useful. You can define a message condition to reduce the number of messages caught by a message interceptor.

interceptor.MessageCondition = new MessageCondition(
  MessageProperty.Body, 
  MessagePropertyComparisonType.StartsWith, "CKF:");

A message condition describes a MessageProperty (such as text message body or sender’s phone number) that should be compared against a specific value. A MessagePropertyComparisionType (such as Contains, EndsWith, StartsWith, or Equal) defines how to perform the comparison.

Only SMS text messages that pass the comparison will be forwarded to the message interceptor. In the code sample above, our message interceptor will intercept any message that begins with the special prefix “CKF:”. Any SMS message that does not match this condition will be placed into the standard SMS inbox.

Transient Interceptors

Now that we have configured the message interceptor we can finally request that it listens for incoming SMS text messages. We do this by registering a MessageReceived event handler.

interceptor.MessageReceived += SmsInterceptor_MessageReceived;

When we no longer desire to listen to incoming SMS text messages we simply remove our event handler.

interceptor.MessageReceived -= SmsInterceptor_MessageReceived;

When the last event handler unhooks itself from the MessageReceived event the message interception process will be disabled.

While enabled any received text message will be passed to our MessageReceived event handler for processing.

void SmsInterceptor_MessageReceived(object sender, 
         MessageInterceptorEventArgs e)
{
  SmsMessage msg = e.Message as SmsMessage;
  if (msg != null)
  {
    // Process the SMS text message 
    ....
  }
}

Persistent Interceptors

One flaw of solely using the MessageReceived event is that if your application exits you will stop intercepting SMS messages.

If you need to continue to intercept messages while your application is shutdown you need to use a persistent message interceptor.

In order to enable persistent message interception you simply need to call the EnableApplicationLauncher method.

string appId = "ChrisTec-SmsDeviceControl";
interceptor.EnableApplicationLauncher(appId);

The appid parameter is a unique string which uniquely identifies this particular message interceptor. No two message interceptors should use the same value. Common suggestions are to use a randomly generated GUID or a string that contains a company or product specific prefix.

To disable persistent message interception you call a matching DisableApplicationLauncher method.

interceptor.DisableApplicationLauncher();

When an SMS that matches the specified message condition is received by the PDA and the application is not running the OS will automatically restart the application. As part of your application’s startup procedure you need to recreate a new MessageInterceptor instance.

The easiest way to create an instance with an identical configuration is to use an additional constructor overload that reads the values out of the registry location used to store the interceptor details while the application is not running.

if (MessageInterceptor.IsApplicationLauncherEnabled(appId))
{
  // Persistent message interceptor has been enabled, so
  // pick up the settings from the registry
  interceptor = new MessageInterceptor(appId);
}
else
{
  // A persistent message interceptor is not enabled, so
  // create a new instance and manually setup the
  // interceptor
  interceptor = new MessageInterceptor(...);
  interceptor.MessageCondition = new MessageCondition(...);
}

As demonstrated in the above code snippet, it is common to see start up code that makes use of the MessageInterceptor.IsApplicationLauncherEnabled method. This method checks if a persistent message interceptor is currently enabled, allowing the application to determine if it needs to register the application launcher or can simply pick up the pre-existing settings.

Sample Application

[Download SmsDeviceControl.zip - 10KB]

A sample application is available for download. The application demonstrates one possible way for a Windows Mobile application to respond to special SMS text messages.

Once the application is ran (and the enabled checkbox is checked) the application will respond to any SMS text message sent to the device that starts with the “CKF:” prefix.

The application parses the contents of the SMS messages it intercepts and interprets them as commands. The following three commands are currently understood:

  • GAME – launches the solitaire game on the PDA
  • HELLO – displays a message box on the PDA’s screen
  • MESSAGE your_message – displays your message on the PDA’s screen

For example an SMS text message containing the text “CKF: MESSAGE Hello World!” would cause the PDA’s screen to display the text “Hello World!”.

If you send an SMS text message with the CKF: prefix that isn’t one of the above commands the application will programatically send an error response message back to the sender.

As SMS text messages are processed you should note that the message counter increases. However if you exit the application and then send an additional text message the counter should restart at 1. This occurs since the counter is not persistent so it does not survive the application re-launch.

9 Responses to “Intercept SMS messages”

  1. victor says:

    hello there:)

    i stumbled on this site while looking for information on how to develop
    database applications for windows mobile6.

    i think it’s one of the more understandable discussions i have found.

    i am researching help topics because i want to learn. i plan to develop a database application in my windows mobile 6 cellphone (it’s a blackjackII).

    but i can’t find a structured help topic that guides a beginner from scratch, and develops knowledge step by step.

    do you have any ideas where i can find that?

    thanks much :)

    victor

  2. Alex says:

    Great tutorial, I only have one problem, the MessageInterceptor doesn’t seem to work at all on certain devices (like my HP).
    Do you ever had that problem and do you know how to solve it?

    Thanks

  3. Richard Cowley says:

    I am trying to learn how the SMS Interceptor works but on my system it does not seem to recognize any incoming SMS messages. It just ignores them as if no message meets the MessageCondition criteria. I have downloaded your application and tested with the same results I was getting with my own.

    Any ideas?

  4. Ziddan says:

    Hi,
    This example is very good and way to describe is fantastic !!

    Great Respect towards your efforts, who spread Good and Valuable knowledge everywhere !!

  5. Amgad says:

    If only I found this post 2 months ago. This post is all you need to learn about SMS Interceptors. Great job.

  6. hann says:

    if i want to Interceptor specical phone number short messge, how to set the condition

  7. Dwarak says:

    Is it possible to intercept outgoing sms

    thanks
    dwarak

  8. Elmasto says:

    Hi,
    This is a good tutorial, especially the downloadable project. Thank you for spreading the word.

  9. [...] I’ve also started playing with automating some of this in response to SMS messages. [...]

Leave a Reply