This is part 1 of a 3 part series where we will get to know how Notification Hubs work and why they are a cool feature of Windows Azure.
You can get the source code of the projects in this blog post here: http://github.com/sabbour/notificationhubw8
Most users of mobile applications now expect to be notified when something of interest to them happens in the app, instead of having to regularly check. So if you are creating a mobile app, chances are you will need to use push notifications functionality sooner or later.
But real world apps have complex needs, whether you are pushing to multiple platforms, requiring personalization and localization of the pushed messages, handling different mobile app versions and most importantly scale.
Push notifications are not as trivial to implement as they may sound, an end-to-end solution requires a lot of backend code that you would have to write and maintain.
Each push notification service (PNS) has different protocols (e.g., HTTP vs. TCP, xml payload vs. JSON payload) as well as different presentation formats and capabilities (tiles vs. toasts vs. badges).
A PNS provides you a way to send a message to a certain device, but doesn’t provide a way to do “broadcast” messages that reach multiple devices. If you need to send a message to multiple devices, such as a breaking news notification, or specific interest groups, such as people who are interested in “Sports” you would have to write that in your backend and maintain a registry associating device handles to interest groups/users.
Storing device handles (which expire), and broadcasting to millions of devices with low latency requires parallelization in both, the backend machines and the database, meaning high storage and machine costs.
Before diving into how Notification Hubs work (and diving into the code :) ) let’s compare how to send a push notification using your own backend vs using the Notification Hubs.
Now why would the Notification Hubs scenario be better, you might ask:
Login to the Windows Azure management portal, click on New, App Services, Service Bus, Notification Hub
In Visual Studio, create a new Windows 8 application, then open the Package.appxmanifest file and turn on “Toast Capable”
In order to use push notifications, your app has to be associated with the Store. Right click on the project, go to Store, click on Associate App with the Store
Sign in with your store account then select an app (or reserve a new one) and continue with the wizard till the app is associated.
Go back to the management portal. and go to the Notification Hub configuration to enter the settings
You will need to get those from the Windows Store dev center under the Dashboard click Edit on your application name
Then go to Services
Then Live Services
Then Authenticating your service
Then pick the Package SID and Client Secret from the page, and put them back into the configuration on the portal
Now that the configuration for the Notification Hub is done, let’s write some code in the Windows 8 app to register with the Notification Hub.
1: private NotificationHub hub;
1: hub = new NotificationHub("[notification hub name]", "Endpoint=sb://[servicebus name space].servicebus.windows.net/;SharedAccessKeyName=DefaultListenSharedAccessSignature;SharedAccessKey=[Your Listen Shared Access Key]");
2: var channel = await PushNotificationChannelManager.CreatePushNotificationChannelForApplicationAsync();
3: await hub.RegisterNativeAsync(channel.Uri);
1: NotificationHubClient hub = NotificationHubClient.CreateClientFromConnectionString(
2: "Endpoint=sb://[service bus name space].servicebus.windows.net/;SharedAccessKeyName=DefaultFullSharedAccessSignature;SharedAccessKey=[notification hub full key]",
3: "[notification hub name]"
1: var toast = @"<toast><visual><binding template=""ToastText01""><text id=""1"">Hello from server!</text></binding></visual></toast>";
We’ve barely scratched the surface here, in my next post, I’ll show you how to use Tags and we will create a Breaking News application that will push news announcements to certain categories to millions of devices, just like how the Bing News app is doing it on its Windows Phone and Windows 8 apps with the Notification Hubs. Stay tuned!