Showing posts with label SMS. Show all posts
Showing posts with label SMS. Show all posts

30 January 2011

Android SMS XML Parsing

Using XML with SMS sounds kinda dumb.  After all an SMS text is only 140 characters (or less ...) and XML is horribly bloated.  But if the originating application is using XML and SMS is the convenient transport, then nothing's too stupid.

There's plenty of articles that describe the various Android options for parsing XML.  SAX, DOM, and XML Pull.  Actually, I'd prefer to use XStream for quick XML read/writing but with the XStream jar around 500k, it's not a good droid choice.

I'm going to quickly show you how I intercept incoming SMS messages and route them to an XML parser if the message starts with a tag (in my case <evt>).  After parsing, I then look at fields in the message and then start other processing that I won't go into here.

I'll show you some quick screen shots of the code and attempt to explain.  At the end of the article, you'll find a link to the entire Eclipse project.  This has been tailored down quite a bit from how I'm using it.  It simply sniffs incoming messages and then displays XML fields.

An example incoming XML SMS message looks like this.  I'm receiving XML SMS messages from several sources such as email (like below) and from Twitter (like I blogged about here and here).


Even though my sample SMS XML message shows line breaks so the XML is nicely formated, you don't want to do this.  Don't waste a precious character on a break.  Run all the XML tags together.  My XML Pull Parser class is expecting no line breaks.

In AndroidManifest.xml, add a receiver for the SMS_RECEIVED action to your application and add RECEIVE_SMS to your permissions:


...

In your receiver class (the one that you named in your manifest file), extend BroadcastReceiver and implement onReceive.  After jumping thru a few hoops to get the message content, determine if this is a message you care about.  My "special" messages all have the XML tag "" after a few header characters depending on the source (such as email or Twitter).  If I find this tag, I display a quick toast and then send the message for XML parsing.

 
Here's an example Eclipse app that parses incoming SMS message and checks if they have the tage.  If so, a toast message is displayed and the sent of for XML parsing.  After parsing, the app simply display the contents of a few of the XML tag contents.

My deployed app is actually an Android service, not a simple app like this example.

Download the example.

What do you think? Leave a comment.

29 November 2010

Working with the 90 Character Limit of Twitter

What?  Twitter has a 90 character limit?  No, it's 140.  Everyone knows that.

Mr Wikipedia says this about SMS length: 

"... resources in the system could be used to transport messages at minimal cost. However, it was necessary to limit the length of the messages to 128 bytes (later improved to 140 bytes, or 160 seven-bit characters) so that the messages could fit into the existing signaling formats."

Using the Groovy code that I did the other day to send a text message via a twitter Direct Message, if the message length is greater than 140 characters, twitter complains with this error:

403:The request is understood, but it has been refused.  An accompanying error message will explain why.
{"request":"\/1\/direct_messages\/new.json","error":"The text of your direct message is over 140 characters."}
TwitterException{exceptionCode=[6bcd7469-01bff100], statusCode=403, retryAfter=0, rateLimitStatus=null, version=2.1.7}


That kinda says that I can send 140 characters, so what's this nonsense about a 90 character limit?

Well, true, you can send 140 characters but if you're hacking the system for the purposes of having Twitter forward your message as a text message to a phone then different rules apply.

Take a look at what happens when you send a Direct Message of 140 characters to an account that is set up to send a txt:
String message = "12345678911234567892123456789312345678941234567895" +
                 "12345678961234567897123456789812345678991234567891" +
                 "1234567891123456789212345678931234567894"

DirectMessage directMessage = twitter.sendDirectMessage(screenName, message)
Twitter breaks the message into 2 separate texts (and my phone occasionally gets the 2nd text first).

For the app that I'm working on, I need my data in a single text message, I don't want the mess of re-assembling.

I think the message split is because a header of:

"Direct from" screen name:

and a footer of

"Reply with 'd" screen name "hi.'"

is added to each message.

I've found that the amount of data that you can actually send depends on the screen name.  Since screen names can be up to 15 chars, you lose:

Header:
Direct from screenname:
= up to 28 characters (12 + screen name)

Footer:
Reply with 'd screenname hi.'
= up to 34 characters (19 + screen name)

140 - 28 - 34 = 78

Uh, 78 is not 90.  What the heck? I guess my math has errors.

But check this out.  By adjusting my direct message, I find that a 90 character message is the sweet spot for a 15 character screen name and the message is sent as a single text.  91 characters will cause the message to be split over 2 texts.

Maybe that header and footer isn't as big as I think it is.

140 - 90 = 50 characters for header/footer?

If a screen name is used twice (2 * 15 = 30), then maybe the bloat is only 20 characters?


What happens if I send messages to a different screen name, one that is smaller than 15 characters?

What is I use a screen name of 8 characters?  Should I be able to send a message of

140 - 20 (bloat) - (2 * 8) = 104 characters?

After a quick reassignment of my phone to a different twitter account, I tried a test with a screen name that is 8 characters.  Could I send 104?  Durn right I could!  And I could also send 109.  But 110 would cause a message split.  So I have an extra 5 characters unaccounted  for.

What happens if I send to a ...  No, this is going on too long. 

The max screen name is 15 characters and that limits the max amount of data that can be sent in one message to 90 characters.

If the screen name is less than 15 characters, then you get 90 plus some extra.  YMMV


What do you think? Leave a comment.

27 November 2010

Send SMS Text Message from Java/Groovy - via Twitter

Writing code to send an SMS text message is fairly easy on a smart phone like an Android.  But from a desktop, sending texts is still a mess, not always free, and/or requires a phone connected to your server.

I simply want to send a message to my phone when my app receives some data.  I don't need a complex library with lots of setup nor do I want to sign up for a pay service.

Now this isn't the cleanest solution to text from an app but Twitter can send messages to your phone when people you follow update their status, when someone mentions or replies to you, or when you get a direct message.

My last two write-ups, "Send a Simple Tweet with OAuth and Groovy" and "Send a Direct Message Tweet with Groovy" showed how easy it is to use Java/Groovy and the Twitter4J library to send messages to your own Twitter account or a message to anyone else.

By sending a tweet to an account that you follow or a Direct Message to your own account, Twitter can be setup to forward that message to a phone that you associate with your account.

Simply go to your Twitter account, Settings, Mobile and follow the easy instruction. Then use one of the sample apps from my previous posts and tweet-text away!

Note:
  • This only allows you to tweet-text to phones that have been setup to follow accounts. 
  • This does not allow you to send a text to a cell phone not registered with Twitter.
  • This works for what I need.
  • Do you know a better/easier, free method?  Write a comment!


What do you think? Leave a comment.