Push Notification using the Windows Azure SDK v2.3
Let's say that we want to create a Windows Phone mobile application that will receive notifications from the App back-end, which will in turn receive notifications from a Notifications Hub. The notifications can be generated from an external API, for instance some kind of System Monitors that monitors a list of servers and returns an XML document containing the results of different checks. We need to have a job running on a scheduled basis that will obtain the checks from the external API, compute a status according to weight associated with each check and then push a notification to a mobile device. If an error occurs, it will be logged using the Semantic Logging Application framework containing the Azure Table Storage Sink. We want to use latest design techniques to make the code as testable as possible, so by using the Unit Of Work pattern (Inversion of Control or "IoC")
It is possible to create a complete mobile notification solution using these Azure services:
Use Azure Notification Hubs to send push notifications from any backend, either in the cloud or on premise, to a Windows Phone 8 application, or any other mobile platform. The goal of the Notification Hub is to provide a platform agnostic notification system, or PNS. It is possible to target individual users or to broadcast to multiple user using tags. For more information on the Notification Hub, take a look at the MSDN documentation here.
Azure Mobile Services
Write your services using the power of ASP.NET Web API. These services will eventually be replaced by the Azure App Services altogether. The Azure App Services include: Web Apps, Mobile Apps, API Apps and Logic Apps.
Azure Table Storage
The Azure Table storage service stores large amounts of structured data.
Azure Mobile Services - SystemHealthNotificationJob
First off, let's take a look on how the job will look like. The job will be set to run at a frequency of every 15 minutes from the integrated scheduler of Azure Mobile Services.
Azure Table Storage - AzureTableLoggingManager
For the logging part of the solution, we will be using the Microsoft.Practices.EnterpriseLibrary.SemanticLogging.WindowsAzure namespace. Here it what the Logging Manager class looks like:
Notification Hub - HealthStatusNotificationProvider
For this part, we will be using the Microsoft.ServiceBus.Notifications namespace. We will be using the CreateClientFromConnectionString method to create a new client to connect to the Azure Notification Hub in the cloud. All you need is the connection string which can be obtained from the Azure Management Portal. In this example, we put it in the Web.Config file under the "MS_NotificationHubConnectionString" name. Now let's take a look how the HealthStatusNotificationProvider is coded. This is where the Unit of Work pattern kicks in.
Using the S.O.L.I.D. principle, that is the first letter for single responsibility, we want to use a Unit of Work class to obtain the application list and one more to obtain the checks from the external API. We will not go into detail about this notion.
Also, let's take some time to examine the business logic. We want to iterate through each application, and each server for each one of these applications. If there is a GlobalStatus that is not green, or not OK, then the notification will have to be sent to the Azure Notification Hub. Here is a class diagram to illustrate the iterative process:
Now let's take a look at the class responsible for sending notifications to the cloud:
Push Notifications - PushNotificationUoW
Now let's take a look on how to create a simple Toast Notification to be sent to a Windows device. We are using a simple factory class to create the different templates of the notifications. Here we have yet again another Unit of Work class to create and sent asynchronously the toast notifications to the Notification Hub in the cloud.
The end result of the CreateNotification method will be a WindowsNotification type object which is from the WindowsAzure.ServiceBus namespace. Once the notification has been sent to the cloud, in a matter of seconds, the hub will dispatch that notification to all subscribed devices. Of course you can configure the subscriptions according to different tags. So there you have it, a simple implementation of a push notification scheduled job for mobile devices.