Application Trigger Messages on Windows Mobile


Application Trigger messages are SMS-based messages targeting specific applications on a mobile phone. These messages can be used to execute applications or perform actions within applications, without explicit user input. This article focuses on the use of these Application Trigger messages to interact with Windows Mobile Pocket PC or Smartphone applications.

  • Take a look at our API homepage for more information on sending SMS messages using Vodafone Betavine.
  • Return to the Application Trigger Homepage for a more detailed introduction to this concept including examples using the J2ME Push Registry.
SMS Message Interception

Windows Mobile SDK (5.0 and up) includes classes for intercepting SMS messages. These classes give developers the possibility to write applications which can be started and manipulated by a SMS message.

Windows Mobile SDK provides the MessageInterception namespace to handle incoming SMS messages.

 

There are two different ways a SMS can be used:

  1. starting an application
  2. passing messages to an application
There is currently no support in Windows Mobile for intercepting Binary SMS Messages targeted at specific port numbers, as used with the Push Registry in J2ME. To overcome this limitation, the approach described here uses a keyword or number inserted in the body of the SMS message to determine which application should be launched.

Resources to help you get started
 
Microsoft Mobile Windows SDK 5.0
Microsoft provides two SDKs for the Windows Mobile 5.0 platform:
Both can be easily integrated into Microsoft Visual Studio 2005. Look also at the MSDN documentation.

Microsoft Mobile Windows SDK 6.0
Microsoft provides a standard and a professional SDK for the Windows Mobile 6.0 platform (Windows Mobile 6 SDK Refresh). Both can be easily integrated into Microsoft Visual Studio 2005. Look also at the MSDN documentation. The SDK includes a Cellular Emulator which must be configured correctly to work with the WM6 mobile device emulator.


Tutorial

The following examples were take from several sources. The code is written in C#.

Including message interception in your application

 

  1. Instantiate a MessageInterceptor object
    MessageInterceptor mi = new MessageInterceptor(InterceptionAction.NotifyAndDelete);
  2. Set a condition, how the SMS must look, which will be used by your application:
    mi.MessageCondition = new MessageCondition(MessageProperty.Body, MessagePropertyComparisonType.StartsWith, trigger);
    Now every SMS starting with the words specified in the string trigger will be intercepted..
  3. Tell the MessageInceptor what to do when a message was intercepted:
    mi.MessageReceived += new MessageInterceptorEventHandler(mi_MessageReceived);
    now mi_MessageReceived will be called when the MessageCondition is met.
  4. Implement  mi_MessageReceived()
    protected override void mi_MessageReceived(object sender, MessageInterceptorEventArgs e)
    {
           // add code to be executed when receiving the specific SMS
    }

 

Registering the application

In order to activate the MessageInterceptor it must be enabled, e.g. the trigger and the application (appPath) must be put in the WM registry:

 

            mi.EnableApplicationLauncher(trigger, appPath);

 

Now when your application is stopped (terminated) and even after a soft reset, a SMS with the specified trigger will start your application.

 

The information for the application launcher is stored in the windows registry. It will be removed through a hard reset, or by executing the following code snippet:

          if (MessageInterceptor.IsApplicationLauncherEnabled(trigger))

          {

                mi = new MessageInterceptor(trigger);

                mi.DisableApplicationLauncher();

          }

 

The trigger from before is needed. This can be done from your application or any other program.


Using the SMSLaunch class

In order to make it easier to write an application using message interception the SMSlaunch class (using Vodafone.betavine namespace) was written.

The classes are available under the project WM_ApplTrigger. It is provided as an Visual Studio 2005 project and as Vodafone.betavine.dll.


SMSLaunch simulates a push registry. Similar to push registry applications it allows to start a message through a SMS. This is done by a trigger string included in the message body. So far it only is to be possible to send text messages using C#. In the SMSLaunch class the trigger string consists of "push: " + a 4 digit port number, e.g. "push: 0000". This trigger is used to identify the application to be launched. Please note that the port is not used as a port in the strict sense, rather as a unique identifier. The term port was "mis"-used as they play a role in MIDP. No actual ports are used in the application.

SMSLaunch extends the Form class and can be used by any application by extending it.  This application needs to the have the following code included:

  1. Call the base constructor of SMSLaunch with a 4 digit string as argument, e.g.
    public myApplication() : base("1234")
    {
           // add code
    }

  2. Implement  mi_MessageReceived()
    protected override void mi_MessageReceived(object sender, MessageInterceptorEventArgs e)
    {
           // add code to be executed when receiving the specific SMS
    }


Example: SMSAlarm

When you send a SMS the receiver will only be notified by the means set in the mobile device. Sometimes it might be useful to get around it and play some soundfile. The example is available under the project WM_ApplTrigger.

using System;
using System.Windows.Forms;
using Microsoft.WindowsMobile.PocketOutlook.MessageInterception;
using Microsoft.WindowsMobile.PocketOutlook;
using Vodafone.betavine;
namespace SMSAlarm
{
    public partial class SMSAlarm : SMSLaunch
    {
        #region PlaySound
        // To play a sound, we need to PInvoke into coredll.dll and call PlaySound
        [System.Runtime.InteropServices.DllImport("coredll.dll")]
        static extern int PlaySound(string pszSound, IntPtr hMod, int fdwSound);
        const int SND_FILENAME = 0x20000;
        const int SND_SYNC = 0x0;
        const int SND_ASYNC = 0x1;
        const int SND_LOOP = 0x8;
        #endregion

        public SMSAlarm() : base("1234")
        {
            InitializeComponent();
        }
     
        protected override void mi_MessageReceived(object sender, MessageInterceptorEventArgs e)
        {
            string theSMS = ((SmsMessage) e.Message).Body;
            string soundFile = ""; // sound to play when event raised, empty = default sound

           // Play the sound in a loop
            PlaySound(soundFile, IntPtr.Zero, SND_FILENAME | SND_ASYNC | SND_LOOP);
            MessageBox.Show(theSMS.Remove(0, 10), "SMS Alert");

            // The message box has been dismissed, stop the sound and exit
            PlaySound(null, IntPtr.Zero, 0);
        }
    }
}

This is just a partial code. The whole project can be downloaded here.

After installing/running it once, the alarm can be invoked by sending a SMS with the content

             push: 1234 Your personal message

this will launch SMSAlarm, play a continuous beeping sound and display a message box with the title "SMS Alert" and the body "Your personal message".

Hint:
When developing, it is useful to include buttons/menu items allowing to disable (deregister) and exit the application. Otherwise you get deployment errors and risk that an old version is used while testing. The project contains menu items for these options.

Unresolved issues

Some inconveniences were encountered while programming the project. There might be trivial solution to solve them, so please post them in the application trigger blog.
  • Forms elements cannot be inherited in Visual Studio 2005. Hence including the above described exit/disable menu items could not be done in SMSLaunch
  • Under C# only text messages can be send. Is it possible to send BLOBs by overloading the SMS protocol.