Intercepting incoming SMS

In Chapter 3, Building the Mobile Sales Force Module, you've created a History tab in the Account Details window that displays logs of all communication with a lead, opportunity, or customer. These logs include incoming phone calls and SMS messages. In this section, you will write the code that generates these logs.

To perform this, you would need to be able to detect SMS messages as they come in. Your application would then need to do the following:

  1. Look at the origin (phone number) of these messages
  2. Locate the lead account with the matching phone number in the sales force application
  3. The log entry is then created under this account (if it is found)

Intercepting an SMS message

Intercepting an SMS message requires a bit more work compared to sending one. The Microsoft.WindowsMobile.PocketOutlook.MessageInterception namespace provided by Microsoft allows you to define a call back function that is automatically called when a new SMS message arrives.

Let's take a look at how this is done in code. The first thing to do is to import the namespace we need. This is shown as follows:

using Microsoft.WindowsMobile.PocketOutlook.MessageInterception;

Next, you need to create a MessageInterceptor object. You can specify one of these two values for the InterceptionAction argument in the constructor of this object:

  • InterceptionAction.Notify

    This allows your application to 'peek' at the incoming SMS message without deleting it—this message is still available to other applications.

  • InterceptionAction.NotifyAndDelete

    If you choose this option, the SMS message will not be available to other applications after your application has received it.

The second argument defines whether to use the form thread to process the events. If your application does not use any form (for example, in the case of a console application), set this value to false.

MessageInterceptor _SmsInterceptor;
_SmsInterceptor = new MessageInterceptor(InterceptionAction.Notify, True);
//You must then add an event handler for the MessageReceived() event //in the interceptor object.
_SmsInterceptor.MessageReceived+= SmsReceivedEventHandler;

Through the event arguments of the event handler, you can retrieve an SmsMessage object, which you can then use to retrieve the originating phone number (or SMS content).

public void SmsReceivedEventHandler(ByVal sender As Object,
ByVal e As MessageInterceptorEventArgs)
{
SmsMessage _msg = (SmsMessage) e.Message;
MessageBox.Show("Received SMS from the following number: "
+ _msg.From.Address.ToString(),"");
}

The MessageInterceptor object seen previously is typically created when your application starts, and you will only receive SMS receipt notifications when your application remains open. The moment you close your application, the object goes out of scope and you stop receiving SMS receipt notifications as well.

If you are planning to process each and every single SMS message that comes into your phone over extended periods of time, it would make more sense to create a window-less application that runs constantly in the background, listening to the events raised by the MessageInterceptor class. You will see how you can do exactly this in the next few sections.

Note

Unfortunately, the MessageInterceptor object cannot be used to intercept an e-mail message. To intercept an e-mail, you would need to invoke MAPI (Messaging API) functionality on the device, which is out of the scope of this book.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.149.254.35