El Cheapo Notification System using Windows Azure Notification Hub and Google Cloud Messaging

The Premise

Most if not all organizations nowadays place a lot of emphasis on timely information dissemination to its team members. On some entities, this timely information, can make a big difference between success and failure. For example:

1.) You have a Surf club, and you want all the members to get the first dibs whenever one of your buddies find out, that the surfing conditions on one particular surf spot are currently awesome. For example, upon seeing the awesome waves, you pull out your smartphone, start up your Surf-Club-App, and post an “alert”, attached with your selfie on the beach ready to dive in.  The other guys get the notification on their smartphones, call in sick, and quickly hit the road to Santa Cruz! Sure you could also email them, but really???

2.) You own the hottest ice cream shop in town, such that every day you are always sold out. You have an App for your most loyal customers, and they can get notifications when a batch of the flavor of the month is ready, and indicating how many scoops are left. They can then come up with silly excuses at work, to rush to your ice cream joint…

Get the drift? OK, here’s a general illustration of what I’m trying to get across…

Presentation1Case Study

OK, enough with the mindless drivel. I’m going to create a real world example based on the infographic above. I am going to implement a traffic incident alert system for the City of Calgary, which happens to be my hometown. In their Open Data website, they published a link, updated every 10 minutes, a KML formatted file (list) of the traffic incidents reported throughout the city.

If you haven’t heard about cloud infrastructure services, a quick assessment of this project would probably lead you to think of the following:

  • A computer, with a cron job running in your basement which polls the traffic incident link every 10 minutes.
  • The same computer, or another one running an application which keeps track of the smartphone registrations of your users, and broadcast the incident alerts to each of them.
  • A general purpose storage system/database

Now, if you think about scalability and redundancy, your basement probably won’t fit the bill. You will probably build at least 2 data centres, with the accompanying IT staff. One on your premises, an one across the continent (redundancy)…. So much for being cheap…

Cloud Services

Fortunately (for me and probably most entrepreneurs, startups), I can do this project without shelling out ANY money upfront just to show a proof of concept. I could instantly scale up if the idea/project actually works and attracts a sizable user base of course.

For this project, I am going to use Windows Azure cloud services.  They have a free tier on their services which jives well with our El Cheapo theme. Also, I had been an early adopter of Windows Azure for use with my hobby projects/experiments, and I can categorically say that their cloud service has improved a lot since the earlier years. This is from a developer point of view.  I had tried Amazon’s cloud service as well, but I didn’t have a good experience back then. I got dinged for what I thought a “trial service”. To Amazon’s credit, they refunded me after the whole kerfuffle, and it was probably a reading comprehension issue on my part with regards to their pricing structure. Google has  a similar offering too, but I hadn’t found the time to play with it yet.

Divide And Conquer

So let’s start to look at the programming tasks, and some leg work that needs to be done.

First, we need to write the program that downloads the KML file, and transforms the data suitable for broadcasting to the smartphone users. And we need a machine/computer to run this program obviously. And I’m not talking about the computer in the basement.

A few weeks ago, Microsoft released the WebJobs add-on to its Websites services. Examining it closely, looks like this will work for our project. There is a free tier in their Websites service which again bodes well for this project.  As the webjobs documentation says, you have a few choices of programming languages to implement your cron job. We will create a Windows Console App (.exe) for this task, using C#, and using another free toolkit, Visual Studio Express (Desktop).

Presentation3

To make this blog shorter, I’m not going to belabor you on how to create a free Website instance on Windows Azure. There are plenty of tutorials and documentation on how to do this. I will leave that to you as an exercise :-) . Needless to say, after you created the free website instance, we now have place to deploy our cron job, so let’s code our console app.

I used the XMLReader class, to download and parse the KML (essentially XML) file from the incidents link. Of course you can probably do a smarter and more robust way of doing this task. I then represented each incident as an IncidentModel object, shown in the class definition below

incidentmodel

As mentioned earlier, this cron job will run every 10 minutes. But does that mean, that every 10 minutes, we will broadcast the data to our smartphone users even if nothing has changed? Hell no! Our users will uninstall the App sooner after experiencing repeated notifications. We have to find a way to detect whether the current download, is the same (or not) as it was 10 minutes ago.

I could probably have designed this cron job, to run on a continuous mode such that I can have an in-memory copy of the snapshot data “10 minutes ago”. I can then implement an internal timer, to poll the incidents link every 10 minutes. Because I have used Websites under the free mode scheme, I suspect that at some point, it will be App recycled. And maybe, when this happens, my cron job, which lives under the WebJobs add-on, will also get rebooted thereby losing the in-memory snapshot. This can cause issues with our project (i.e. our smartphone users will get the same notifications). As an aside, there is an “always on” setting for the non-free subscriptions. This will probably make continuous mode work. Something to explore…

So yes, we need to devise a way to store our snaphot data. I could use the free 20MB SQL storage that came with the Websites service. But in planning for the supposedly long term expansion, after you breach the 20MB mark, the costs can go significantly higher starting at about $5 a month! Look, I’m not implying that at some point, Calgary will have so many accidents in a span of 10 minutes such that it will require 1GB of storage to store the data. I’m trying to drive home the point of planning for scalability. Just sayin’…. Besides, I don’t want to waste any more lines of code needed to make this project work, as I only have so many keystrokes left… I could just store the downloaded XML, the way as it is, and then 10 minutes later, read it,  and compare  it with the fresh download.

The operative word in the previous sentence was store. How do I store a file in the cloud? There is a cloud service for that too! Check out Windows Azure Blob Storage Service.

So anyway, long story short, here’s the cost comparison between using Blob storage and SQL Database

Presentation4

There is no way that the city of Calgary, will have so many accidents within 10 minutes to make the KML file grow to a size of 1 terabyte. So I’m going to use the Blob Storage approach.

Notifications

Assuming the cron job above works well, we now need to add the missing piece of the puzzle – sending out the notifications to our smartphone users. Since I own an Android phone, I only implemented the notification for Android devices. To send notifications to Android devices, one of the ways to accomplish this, is to use the Google Cloud Messaging service. And guess what, it is free!!!

So how do we send out notifications from our cron job to Google Cloud Messaging? If you pour through the GCM documentation,  looks like we need to do a few things:

  1. Create a third-party App server/application whose job is to post messages to the GCM Connection Servers
  2. Create the Android App (client) to receive the notifications
  3. Register our App to use the GCM service.

Item #3 is actually the easiest part. I will leave that to you as an exercise… :-)

Now let’s look at item #1. As you read the documentation, it seems like a tall order, for this weekend project.  Looking at the example codes, sure you could hack your way though the java code of the demo server app, and port them into C# , and into our C# cron job…. Again, I only have finite keystrokes left. There has to be a better way!!!! Two words…

Notification Hubs

Lucky me, I had been playing Windows Azure for a while, and this service (with FREE tier of course) comes in very handy. They even have a how-to-do-it implementation example for Android, from which I can (and you can too) base item #2 task above!

So our cron job C# code, based on the example implementation mentioned above, will now look like what is shown below. See? only a few lines of code!

If you are a security freak, thinking that somebody (you know who) might actually read your surfing alert notifications as it goes through the all the cloud infrastructure (Microsoft & Google’s), you can encrypt your messages here (just to give them a little bit of a hard time), and then decrypt the message on your client App… Food for thought.

croncode

The best part, is that if I have enough spare time, I can also implement this traffic incident notification system, to iOS devices, and Windows Phone devices. See the config screen below. It’s just waiting for the appropriate credentials from each respective platform.

notificationsetupSummary

As you can probably tell, I had already implemented this El Cheapo traffic incident notification that I am preaching here, on Android devices (i.e. my smartphone) , trying as hard as I can to get away with it for free (for now). You can follow a similar path for a notification system project that you have in mind. If this was the 80’s, this project would probably spawn one of the startup companies. Unfortunately, it is already year 2014 now. But I still hope that this blog post will give you some ideas in implementing your own cloud-based system. It’s worth trying.

Plug alert!!!! The Android(TM) app is now available on Google Play. The accompanying promo website is here, free of course courtesy of the Windows Azure Websites service.


Get it on Google Play

Legal Stuff

Disclaimer:The opinions expressed herein are my own personal opinions and do not represent my employer’s view in any way.Other than being a citizen of the City of Calgary (a.k.a. tax payer), I am not connected to the City of Calgary (corporate entity) in any way

I am not in any way connected with Google and Microsoft. To the best of my knowledge, I do not have any direct investments to both Google and Microsoft

Posted in Android, Cloud Infrastructure, Push Notification, Windows Azure