Delivering push notifications to millions of devices with Windows Azure Notification Hubs

Push notifications are a vital component of connected apps. They allow back-end services to send frequent and timely updates to specific users and devices that can then appear as tile updates, toast notifications, badge updates, and more. (See the three-part series, Alive with Activity for more background.) As such, push notifications are perhaps the most powerful customer engagement mechanism available to app developers across phones, tablets, and PCs.

There are many tools that help you implement push notifications (Windows Azure Mobile Services for instance already includes such a feature) more easily. Using these tools to send a single push notification message to one mobile user is relatively straight forward. However, sending simultaneous push notifications in a low-latency way to millions of mobile users, and handling real world requirements such as localization, multiple platform devices, and user personalization is much harder.

The main reason this is challenging is that push notifications are delivered to the devices by platform-specific services. For instance, you have to use the Windows Notification Service (WNS) to push notifications to Windows Store apps, Microsoft Push Notification Service (MPNS) to push to Windows Phone, Apple Push Notification service (APNs) for iOS, and Google Cloud Messaging (GCM) for Android.

All these platform services work by having the device app request a user-, device-, and app-specific ChannelURI and then store it somewhere in the app back-end. Then, when sending a notification, the app back-end posts the notification payload to the ChannelURI in order to reach that particular device. To complicate matters, the ChannelURIs expire (and thus have to be continuously re-uploaded to the back-end) and multiple ChannelURIs can be active at the same time for the same device. When coding multi-platform apps, this problem is intensified because each platform handles push notifications in a slightly different way.

push-fig1

Push notification lifecycle

Assuming you want to send notifications about breaking news, and each user can subscribe to different categories, you will inevitably end up managing (as a database table for instance) the following information:







Device ID Platform ChannelURI Categories Language ClientVersion
1 Windows http://channel.uri/1 {“World”, “Business”} English V1.1
2 WindowsPhone http://channel.uri/2 {“Technology”, “Sports”} French V2.0
   

It’s clear that if you want to allow users to have multiple devices, and if you want to start handling localization and user preferences, this approach can quickly become problematic.

Also, assuming you have all the code to manage the table above and to contact the appropriate notification service for the device’s platform to push notifications, you might still have additional problems if your app (hopefully!) is successful. If you have thousands of devices to notify, having a process go through a for loop won’t give your users a great experience. This means, for example, the latency of the notification will quickly become unacceptably long, and you won’t be able to recover in case something happens to your back-end in the middle of a push. Clearly, while you can solve these problems by storing your device information in multiple databases (i.e. sharding), creating multiple virtual machines that send notifications in parallel, and periodically saving your progress to persistent storage while sending notifications to large numbers of devices, it’s a hard task to implement these patterns in yet another part of your back-end!

Summarizing, we just saw how implementing a real world push notification solution in your app can quickly become large and complex. The biggest challenge is managing device information (especially for cross-platform and/or localized apps) and scaling up of the push infrastructure to handle up to millions of devices. Now let’s take a look at how Windows Azure Notification Hubs simplify this process for you.

Why Notification Hubs?

Windows Azure Notification Hubs provide you with an extremely scalable push notification infrastructure that helps you efficiently route cross-platform, personalized push notification messages to millions of users:

Device token management. Notification Hubs relieve your backend from having to store and manage channel URIs and device tokens used by Platform Notification Services (WNS, MPNS, Apple PNS, or Google Cloud Messaging Service). We securely handle the PNS feedback, device token expiry, etc. for you.

Efficient tag-based multicast and pub/sub routing. Devices can specify one or more tags when registering with a Notification Hub. This indicates a user’s interest in notifications for a set of topics (favorite sport/teams, geo location, stock symbol, logical user ID, and so on). These tags don’t need to be pre-provisioned or disposed, and provide a very easy way for apps to send targeted notifications to millions of devices with a single API call, without you having to implement your own per-device notification routing infrastructure.

Highly personalized. Notification Hubs offer a built-in templating functionality that allows you to let the client choose the shape, format, and locale of the notifications it wants to see, while keeping your backend code platform independent and clean.

Extreme scale. Notification Hubs are optimized to enable push notification broadcast to millions of devices with low latency. A single Notification Hub can handle up to 5 million devices out-of-the-box, and your server backend can fire one message into a Notification Hub, to deliver millions of push notifications automatically to your users, without you having to re-architect your whole app.

Cross-platform. With a single API call using Notification Hubs, your app’s backend can send push notifications to your users running on Windows Store, Windows Phone 8, iOS, or Android devices.

Usable from any backend. Notification Hubs can be easily integrated into any back-end server app using .NET or Node.js SDK, or easy-to-use REST APIs. It works seamlessly with apps built with Windows Azure Mobile Services. It can also be used by server apps hosted within IaaS Virtual Machines (either Windows or Linux), Cloud Services or websites.

Bing News: Using Windows Azure Notification Hubs to deliver breaking news to millions of devices

One of the apps that started to use Windows Azure Notification Hubs since the Public Preview launched last January is the Bing News app included on all Windows 8 and Windows Phone 8 devices. The Bing News app needs the ability to notify their users of breaking news in an instant. This can be a daunting task for a few reasons:

Extreme scale. Every Windows 8 user has the News app installed, and the Bing backend needs to deliver hundreds of millions of breaking news notifications to them every month

Topic-based multicast. Broadcasting push notifications to different markets, based on interests of individual users, requires efficient pub sub routing and topic-based multicast logic

Cross-platform delivery. Notification formats and semantics vary between mobile platforms, and tracking channels/tokens across them all can be complicated

Windows Azure Notification Hubs turned out to be a perfect fit for Bing News. And with the most recent update of the Bing News app, they now use Notification Hubs to deliver push notifications to millions of Windows and Windows Phone devices every day.

push-a

The Bing News app on the client obtains the appropriate ChannelURIs from the Windows Notification Service (WNS) and the Microsoft Push Notification Service (MPNS), for the Windows 8 and Windows versions respectively. It then registers them with a Windows Azure Notification Hub. When a breaking news alert for a particular market has to be delivered, the Bing News app uses the Notification Hubs to instantly broadcast appropriate messages to all individual devices. With a single REST call to the Notification Hub, they can automatically filter the customers interested in the topic area (e.g. sports update) and instantly deliver the message to millions of customers:

push-fig2

Bing News push notifications architecture overview

Windows Azure handles all the complex pub/sub filtering logic for them, and efficiently handles deliver of the messages in a low-latency way.

Start using Notification Hubs

You can try the new Notification Hub support in Windows Azure by creating a new Notification Hub within the Windows Azure Management Portal. You can create one by selecting the Service Bus Notification Hub item under App Services in the New dialog:

push-b

Creating a new Notification Hub takes less than a minute, and after it’s created, you can open its dashboard to see activity.  Among other things, you can see how many devices have been registered with it, how many messages have been pushed to it, how many messages have been successfully delivered via it, and how many have failed:

push-c

Then just configure your Windows Store credentials for push notifications in the Configure tab:

push-d

And your Notification Hub is ready to go.

In your Windows Store app, using the Windows Store device SDK, you can register to your Notification Hub simply calling:

await hub.RegisterNativeAsync(channel.Uri, new string[] { "myTag", "myOtherTag" });

Notification Hubs don’t have an official WinJS SDK yet, but you can download a sample that shows how to use the public REST API to perform the exact same operation, with the following snippet:

hub.registerApplicationAsync(channelUri, ["myTag", "myOtherTag"]);

And then you can have your .NET backend broadcast a message to all your clients, just by calling:

var toast = @"<toast><visual><binding template=""ToastText01""><text id=""1"">Hello everybody!</text></binding></visual></toast>";

await hub.SendWindowsNativeNotificationAsync(toast);

Notification Hubs take any xml payload accepted by WNS (even the new Windows 8.1 templates). To send a tile notification, just build the correct xml payload following the Windows guidelines and use the same method as above (you’d add another <binding> element for the optional 310×310 size as well):

var tile = @"<tile>";

tile += @"<visual lang=""en-US"">";

tile += @"<binding template=""TileWide310x150ImageAndText01"">";

tile += @"<image id=""1"" src=""ms-appx:///images/redWide.png""/>";

tile += @"<text id=""1"">Hello World! My very own tile notification</text>";

tile += @"</binding>";

tile += @"<binding template=""TileSquare150x150Text04"">";

tile += @"<text id=""1"">Hello World! My very own tile notification</text>";

tile += @"</binding>";

tile += @"</visual>";

tile += @"</tile>";

hub.SendWindowsNativeNotificationAsync(tile);

Additionally, we have device SDKs for Windows Phone 8, Android, and iOS. Also, you can use Notification Hubs from your .NET backends, Windows Azure Mobile Service, any other backend with our Node.js SDK, and REST surface.

When you have built your app, scale it to millions of users directly from the Windows Azure management portal.

push-e

And access dozens of metrics about your notifications:

push-f

Finishing up

With Notification Hubs, you can add push notifications to your app without coding any device management or cross-platform code. Quickly set up interest groups and broadcast to millions of devices with no extra work. Start exploring Notification Hubs right now on the Notification Hubs service page, or jump directly into the Getting Started with Notification Hubs tutorial and video. If you don’t already have a Windows Azure account, you can sign up for a free trial and start using it today.

– Elio Damaggio, PM, Windows Azure Notification Hubs (@eliodamaggio)