Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Building A Test Texting System On Windows Phone With TextBelt API

DZone's Guide to

Building A Test Texting System On Windows Phone With TextBelt API

· Mobile Zone
Free Resource

Download this comprehensive Mobile Testing Reference Guide to help prioritize which mobile devices and OSs to test against, brought to you in partnership with Sauce Labs.

There aren't that many applications in the Windows Phone Marketplace that actually work with texting on a level beyond the SmsComposeTask class. There are, however, services like Twilio that allow developers to directly invoke texting capabilities outside the carrier gateway bound to the user. One drawback, though, is that services like Twilio are not free. 

That's when TextBelt comes into the game. It is a very simple API that allows developers to send text messages in the United States with a simple POST call. Of course, I had to create a pretty nice sample application to demonstrate the concept, but here is what it is at its core:

public static class TextBelt
{
    private static Action<string> OnCompletion;
    public static void SendText(string number, string message, Action<string> onCompletion)
    {
        OnCompletion = onCompletion;

        string content = "number=" + number + "&message=" + HttpUtility.UrlEncode(message);
        byte[] data = Encoding.UTF8.GetBytes(content);

        HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://textbelt.com/text");
        request.ContentType = "application/x-www-form-urlencoded";
        request.Method = "POST";

        request.BeginGetRequestStream(result =>
            {
                using (Stream dataStream = request.EndGetRequestStream(result))
                {
                    dataStream.Write(data, 0, data.Length);  
                }

                request.BeginGetResponse(stringResult =>
                {
                    HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(stringResult);
                    using (StreamReader reader = new StreamReader(response.GetResponseStream()))
                    {
                        string serverReturned = reader.ReadToEnd();
                        OnCompletion(serverReturned);
                    }
                }, null);

            }, null);

        
    }
}

The data is sent via a request to http://textbelt.com/text. Notice that I am using HttpUtility.UrlEncode to escape some special characters that will be lost if you attempt to send the plaintext string. The rest is standard response handling and writing data to the request stream. If you've used .NET web requests before, this is a very basic harness.

With some UI work, I got it to look somewhat close to the stock OS messaging application.

When messages arrive on the target phone, their format looks like this:

There are limitations you have to be aware of, implied if you are using this API:

  • You cannot attach media to the message
  • You cannot specify directly who the sender is
  • You are limited to 75 messages per day
  • 3 SMS per number in 3 minutes

It should not be used in production. Its main purpose is to test SMS interactions that can later be implemented via services that offer full messaging support.

You can download the sample app here (it is using the Coding4Fun Toolkit for Windows Phone).

Analysts agree that a mix of emulators/simulators and real devices are necessary to optimize your mobile app testing - learn more in this white paper, brought to you in partnership with Sauce Labs.

Topics:

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}