The Windows Azure Toolkit for Windows 8 is designed to make it easier for developers to create a Windows Metro style application that can harness the power of Windows Azure Compute and Storage. It includes a Windows 8 Cloud Application project template for Visual Studio that makes it easier for developers to create a Windows Metro style application that utilizes services in Windows Azure. This template generates a Windows Azure project, an ASP.NET MVC 3 project, and a Windows Metro style JavaScript application project. Immediately out-of-the-box the client and cloud projects integrate to enable push notifications with the Windows Push Notification Service (WNS). In Addition, the Windows Azure project demonstrates how to use the WNS recipe and how to leverage Windows Azure Blob and Table storage.
The Windows Azure Toolkit for Windows 8 is available for download.
Push Notification Cloud Service Architecture
For those of you who are familiar with working with Windows Phone 7 and the Microsoft Push Notification Service (MPNS), you will be happy to know that the Windows Push Notification service (WNS) is quite similar. Let’s take a look at a birds-eye architectural view of how WNS works.
The process of sending a notification requires few steps:
- Request a channel. Utilize the WinRT API to request a Channel Uri from WNS. The Channel Uri will be the unique identifier you use to send notifications to an application instance.
- Register the channel with your Windows Azure cloud services. Once you have your channel you can then store your channel and associate it with any application specific data (e.g user profiles and such) until your services decide that it’s time to send a notification to the given channel
- Authenticate against WNS. To send notifications to your channel URI you are first required to Authenticate against WNS using OAuth2 to retrieve a token to be used for each subsequent notification that you push to WNS.
- Push notification to channel recipient. Once you have your channel, notification payload and WNS access token you can then perform an HttpWebRequest to post your notification to WNS for delivery to your client.
Fortunately, the Windows Azure Toolkit for Windows 8 accelerates development by providing a set of project templates that enable you to start delivering notifications from your Windows Azure cloud service with a simple file new project experience. Let’s take a look at the toolkit components.
Toolkit Components
The Windows Azure Toolkit for Windows 8 contains a rich set of assets including a Dependency Checker, Windows Push Notification Service recipe, Dev 11 project templates, VS 2010 project templates and Sample Applications.
Dependency Checker
The dependency checker is designed to help identify and install those missing dependencies required to develop both Windows Metro style apps on and Windows Azure solutions on Windows 8.
Dev 11 Windows Metro style app
The Dev 11 Windows Metro style app provides a simple UI and all the code required to demonstrate how to request a channel from WNS using the WinRT API. For example, the following listing requests a Channel URI from WNS:
var push = Windows.Networking.PushNotifications;
var promise = push.PushNotificationChannelManager.createPushNotificationChannelForApplicationAsync();
promise.then(function (ch) {
var uri = ch.uri;
var expiry = ch.expirationTime;
updateChannelUri(uri, expiry);
});
Once you have your channel, you then need to register this channel to your Windows Azure cloud service. To do this, the sample app calls into updateChannelUri where we construct a simple JSON payload and POST this up to our WCF REST service running in Windows Azure using the WinJS.xhr API.
function updateChannelUri(channel, channelExpiration) {
if (channel) {
var serverUrl = “https://myservice.com/register”;
var payload = { Expiry: channelExpiration.toString(),
URI: channel };
var xhr = new WinJS.xhr({
type: “POST”,
url: serverUrl,
headers: { “Content-Type”: “application/json; charset=utf-8” },
data: JSON.stringify(payload)
}).then(function (req) { … });
} }
VS 2010 Windows Azure Cloud Project Template
The Windows Azure Cloud project provided by the solution demonstrates several assets for building a Windows Azure service for delivering push notifications. These assets include:
1. A WCF REST service for your client applications to register channels and demonstrates how to store them in Windows Azure Table Storage using a TableServiceContext. In the following code listing you can see the simple WCF REST interface exposed by the project.
[ServiceContract]
public interface IWNSUserRegistrationService
{
[WebInvoke(Method = “POST”, BodyStyle = WebMessageBodyStyle.Bare)]
void Register(WNSPushUserServiceRequest userChannel);
[WebInvoke(Method = “DELETE”, BodyStyle = WebMessageBodyStyle.Bare)]
void Unregister(WNSPushUserServiceRequest userChannel);
}
2. An ASP .NET MVC 3 portal to build and send Toast, Tile and Badge notifications to clients using the WNS recipe.
3. An example of how to utilize Blob Storage for Tile and Toast notification images.
4. A Windows Push Notification Recipe used by the portal that provides a simple managed API for authenticating against WNS, constructing payloads and posting the notification to WNS.
using Windows.Recipes.Push.Notifications;
using Windows.Recipes.Push.Notifications.Security;
…
//Construct a WNSAccessTokenProvider which will accquire an access token from WNS
IAccessTokenProvider _tokenProvider = new WNSAccessTokenProvider(“ms-app%3A%2F%2FS-1-15-2-1633617344-1232597856-4562071667-7893084900-2692585271-282905334-531217761”, “XEvTg3USjIpvdWLBFcv44sJHRKcid43QXWfNx3YiJ4g”);
//Construct a toast notification for a given CchannelUrl
var toast = new ToastNotification(_tokenProvider)
{
ChannelUrl = “https://db3.notify.windows.com/?token=AQI8iP%2OtQE%3d”;
ToastType = ToastType.ToastImageAndText02;
Image = “https://127.0.0.1/devstoreaccount1/tiles/WindowsAzureLogo.png”;
Text = new List<string> {“Sending notifications from a Windows Azure WebRole”};
};
//Send the notification to WNS
NotificationSendResult result = toast.Send();
5. As you can see the Windows Push Notification Recipe simplifies the amount of code required to send your notification down to 3 lines.
The net end result of each of these assets is a notification as demonstrated in the below screenshot of a Toast delivered using the Windows Azure Toolkit for Windows 8.
As an exercise, it is recommended to spend some time using the website to explore the rich set of templates available to each of the Toast, Tile and Badge notification types.
Sample applications
At present there are also two sample applications included in the toolkit that demonstrate the usage of other Windows Azure features:
- PNWorker: This sample demonstrates how you can utlize Windows Azure Storage Queues to offload the work of delivering notifications to a Windows Azure Worker Role. For more details please see the CodePlex documentation.
- ACSMetroClient: An example of how to use ACS in your Windows Metro style applications. For more details please see this post by Vittorio Bertocci.
- Margie’s Travel: As seen in the demo keynote by John Shewchuk, Margie’s Travel is a sample application that shows how a Metro style app can work with Windows Azure. For more details please see this post by Wade Wegner. This sample application will ship shortly after the //build conferene.
Summary
The Windows Azure Toolkit for Windows 8 provides developers a rich set of re-useable assets that demonstrate how to start using Windows Azure quickly from Metro style applications in Windows 8. To download the toolkit and see a step by step walkthrough please see the Windows Azure Toolkit for Windows 8.
Nick Harris is a Technical Evangelist for Windows Azure. Follow Nick at @cloudnick.