Triggering certain actions such as sending notifications or alerts when a device enters or leaves an area is often referred to as geo-fencing. The geo-fence, the boundary of the area of interest, can be dynamic like a radius around a school or around your own device, it can be pre-defined such as a neighborhood, city or county, or it can be an area defined and digitized for a specific purpose.
For this example we narrow down the many different use cases to location-based notifications that leverage custom boundaries and address scenarios, such as
- A parent who wants to receive a notification when his or her child arrives at school or is safely back home
- Local news that want to send notifications or alerts to only the devices that are within a pre-defined area
- Stores that want to advertise specials and promotions to individuals in the proximity of the store
To enable such scenarios we will leverage the Azure Mobile Services, which provide amongst others authentication, scheduling, and push-notifications, along with SDKs for Windows, Windows Phone, iOS, Android and HTML. No matter what platform, the Azure Mobile Service have you covered. We will also leverage the Bing Spatial Data Services (SDS), which allow you to batch-geocode and reverse-geocode your data, store your geospatial data in the cloud, and query them through a set of APIs. Additionally, the Bing SDS also provide an API to retrieve pre-defined boundaries.
In this end-to-end scenario we will look at the Bing SDS to store and query geo-fences, the tracked device, the Azure Mobile Services as the hub to process the tracked locations and send notifications as well as a Windows Store app to catch and display tile and toast notifications. Extending the notifications to a smart-watch could be an interesting add-on but we will save this for a later post.
For this example I have created geo-fences that cover Microsoft offices in Bellevue, WA.
For the purpose of this blog post we will assume that we have already uploaded the geo-fences to the Bing SDS. Creating the geo-fences and uploading them to the Bing SDS is a topic that we can cover in a separate blog post.
To query the geo-fence that a location is in, we can execute a query such as
The response will contain the geo-fence with its geography described as Well Known Text (WKT), its name, centroid and some other information as described here.
The array of results will be empty if the location does not fall into a geo-fence.
Setting Up the Azure Mobile Services
Azure Mobile Services come with a free tier for up 10 services. So we go ahead and create a new Mobile Service with a new database. If you already use your free database you can also add new tables to an existing database. A great tutorial on getting started with Azure Mobile Services is here. Once we have created the new service we can either download a starter app for several platforms or build a new app from scratch. We will do the latter and will require the URL to the service as well as the application key. We can get both from the dashboard in the Azure Portal.
While we are in the Azure Portal we also switch over to the Data tab and create a new table ‘Tracker’. For this tutorial we keep it simple and don’t change the default permissions.
We don’t need to add any columns either since the database schema is by default dynamic and Azure Mobile Services will automatically create columns based on the data it receives from the client.
We will come back later to the Azure Portal to monitor our apps and add additional functionality but for now we can move on to the mobile app for the devices that we are tracking. In this tutorial we will build a Windows Phone 8 app but as mentioned before Azure Mobile Services support also the Android and iOS platforms – directly through native code as well as through cross-platform solutions such as Xamarin or PhoneGap.
Creating the Windows Phone App
Rather than creating a tracking application from the start we will build upon a sample application for Windows Phone 8 that lets location tracking applications run in the background. For the purpose of this walk through we assume that you have completed the app as described in the tutorial.
We start modifying this existing application by adding the NuGet package for Azure Mobile Services. This will also automatically add a few dependencies.
Next we open the WMAppManifest.xml and add permit the app to retrieve the unique device ID in order to identify each tracked device.
In the App.xaml.vb we import the Azure Mobile Services namespace
And then declare the connection to the Azure Mobile Service with its URL and App ID that we retrieved from the Azure Portal.
In Page2.xaml.vb we import a few namespaces and create a class ‘Tracker’ with properties that we want to submit to the Azure Mobile Services.
In the class Page2 we retrieve the table ‘Tracker’ from the Azure Mobile Service and declare a string-object that will hold our unique device ID. We will read this unique device ID into the string object when the page is first initialized.
Next we add a new dispatcher to the sub geolocator_PositionChanged as shown in the last line of the code-block below.
The function that we call from the dispatcher as well as the actual insert into the Azure Mobile Services will also be added to the class.
For the purpose of this tutorial we’re done with this app. Let’s run it in the emulator and change the position a few times:
Now let’s double check in the Azure Portal. We open the table ‘Tracker’ under the tabulator data and verify that the columns have indeed been created and that we have some records in the table.
You will find the complete source code here on my OneDrive. Note: you will need to edit App.xaml.vb to add your own Azure Mobile Service URL as well as your own App ID.
Creating the Windows Store App
Next we follow the tutorial Get started with push notifications in Mobile Services. Let’s check that everything works well so far by adding a few items in the sample app and verify that we receive notifications.
Bringing it All Together With the Scheduler
We will bring now all the different pieces together with our Scheduler in the Azure Mobile Services. Let’s create a new scheduler in the Azure Portal and plan to run it every 5 minutes but not activate it yet. We can always click on ‘Run One’ to test the code.
Now that we created our job we can navigate to the scheduler job and the script that we want to create. Again we keep it very simple here. There could be a lot of logic to filter messages by tracked device or type of alert (see Next Steps at the end of the tutorial Get started with push notifications in Mobile Services) but in this case we just verify if the last tracked location was inside a geo-fence, then we build toast- and tile-notifications each with a mini-map for that location created from the Bing Maps REST Imagery Services. You will find the full script below.
Let’s start the mobile app and move a location into one of the geo-fences.
Now we Run the scheduler job once and see our tile and toast notifications come in.
Related blog posts:
- Retrieving Boundaries from the Bing Spatial Data Services
- Traffic Notifications with Bing Maps & Azure Mobile Services
- Introducing Support for Custom Geospatial-Data in Bing SDS
- How to Extend Your App with Talking Maps
- How to Create a Customer Ranked Auto Suggest with Bing Maps and Azure Mobile Services
And that’s it for now. Happy coding and we hope to see you soon back here.
- Bing Maps Team