Pushing Rich message content to Samsung Galaxy Android Mobile Phones

Recently as a Part of Developing a mobile messaging service targeted for marketing campaigns on the Android Mobile Phones, I had to generate visually rich content that I had designed in a website and then send that as a payload to a messaging system that accepts the information and pushes the same to Android phone where the custom code I had written on the Phone processed the delivered message in an Inbox and upon clicking it opens/renders a nice looking message screen to the User.

Below is a Webcam shot of the demo. app. (Message Inbox and the Rich Screen in my Samsung Galaxy II Android Phone)


I thought, I would share some info and some code on how I did a part of this  as below .,what I want to convey is how one can dynamically assemble  visually rich content assets (a website  with images or video for marketing campaign or Ad in this case) that is created by a visual designer and pro-grammatically send it to a android push service such as Google’s C2DM

Basically, A Simple Advt./Marketing campaign web  Interface that I developed for testing the App. sends the message subject, the URL from which It has to extract the HTML content  and once you submit this, here is the typical steps/process involved …(pseudo code) before I delve in to the code

Step 1.   Sendmessagetocloudbasedpushservice () {

Send Campaign subject, campaign Rich URL , campaign content etc


Step 2.   Receivemessagecontentsandprepareapayloadtomessageserver () {

a. Retrieve the incoming message contents from the http request
b.  Store the campaign subject and content text from above
c.   Get the URL link where I have published the “Rich” web content page
d.  Open the URL and Scrape the html page
e.  clean the raw HTML extracted /Prettify
f.   Encode the cleaned HTML (escape html)
g. put the encoded html inside a string
h. make it ready for sending it to a message server (make it a part of a XML  file payload specified  ) along with details cited at [b.] above


Step 3.  Createawebservicerequest-to-the-mobile-messageserver () { 

b.send the xml payload
c. retrieve the service response and act accordingly 

I intially thought implementing this is going to be a fairly complex one, But using the HTML Cleaner ,Groovy XML Slurper and XML Converter libraries with Groovy development, It was a breeze and I just got the whole thing working in about an hours time! of course this involved a 45 mins testing , debugging and fixing etc.
import org.htmlcleaner.*
import org.htmlcleaner.HtmlCleaner;
import org.htmlcleaner.CleanerProperties;
import org.htmlcleaner.DomSerializer;
import groovy.xml.XmlUtil;

def  campaignMessage = params

 def xtifyRichURL =campaignMessage .get(‘richurl’)

 // Open the URL Link,Get the page and Parse the HTML page at the specified URL

def URL incomingUrlLink = new URL (“${xtifyRichURL}”)

// Get the HTML Cleaner library do the work of parsing and cleaning

def props = new CleanerProperties()
// set some properties to non-default values

// do parsing
TagNode tagNode = new HtmlCleaner(props).clean(
new URL(“${xtifyRichURL}”)

// serialize the Tagnode to a Html file 
def xmlFile = new PrettyHtmlSerializer(props).writeToFile(
tagNode, “sankartest.html”, “utf-8”

// Now parse the html file written by the html cleaner by slurpur that returns a gPathresult type . (BTW, I tried using the APIs of HTML Cleaner to do this and didnt succeed for 15 mins and being impatient,got it quickly working with the XMLSlurpur -:)

def myHtmlMessage = new XmlSlurper().parse(new File(‘sankartest.html’));
//use the groovy xml utility class and its serialize method to read the gPathResult and assign it with a string by name  gHtmlString

def xmlutil = new XmlUtil()
def gHtmlString = xmlutil.serialize(myHtmlMessage)

//Now Encode the HTML file that was just retrieved ,cleaned and serialized

def encodedHtml = “${gHtmlString.encodeAsHTML()}”
encodedHtml = “”+”${encodedHtml}”+””

Finally, I passed the encodedHtml as a part of the XML element to the message server…

Thats it!

My take away and learning (Mind you , I was doing Java development 10+ years ago back in action on the cloud .So, please bear with me on my code if you are a Java Geek!) .


About Cloudshoring

Cloudshoring is a popular blog on Cloud computing and Big data
This entry was posted in Cloud Computing and tagged , , , , , , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s