One of the things that has always been missing from Azure Mobile Services was the ability to support selective mass notifications out of the box. You could do it, but involved a great deal of extra coding. It made the idea of using Mobile Service for a large scale system less than attractive versus solutions like Parse. Thankfully we know have Azure Notification Hubs to solve this problem.
What is it?
Using a third party service for Push Notifications and client tracking is nothing new, its a very common need when supporting mobile applications. Interfacing with Google, Windows, Windows Phone, or Apple push notification systems can be a very error prone and tedious process (especially as Windows Store apps support upwards of 40 notification templates). This is where Notification Hubs comes in. You allow the client to register with it and it handles the notification through a REST based API. Here is a quick example:
Go out to the Azure Portal and create a new Notification hub (side note: creation of a mobile service will also create a notification hub). The Notification Hub option can be found under App Services –> Service Bus –> Notification Hub
Once the Notification Hub is created take note of the following pieces of information: DefaultListenSharedAccessSignature and DefaultFullSharedAccessSignature under Connection Information. We will be using these values within the Windows Store application.
Crack open Visual Studio and create a new Windows Store app. Make sure you associate the app with an app instance in our Windows Developer account (https://appdev.microsoft.com/StorePortals/en-us/Home/Index). You can do this entire process from the wizard in Visual Studio (right click the project file and select ‘Associate…;’’). Next, open the Package.appmanifest file and ensure that notification (Toast for this example) are turned on.
In order to receive notifications we only need to have the app installed, so no UI is necessary.
What is important is that we register this new “device” with the Notification Hub. While this can be done through the REST API, there is a Nuget package which makes this process much easier. Be careful however, there are two of the, and the one we want is indicated in the picture below:
The key thing here is to look for the word ‘managed’ in the title. That is the one you will need. To register the client, the follow code needs to be placed somewhere (could be in App.xaml or anywhere else).
_hub = new NotificationHub("<hubname>", "<connection string>"); var channel = await PushNotificationChannelManager.CreatePushNotificationChannelForApplicationAsync(); await _hub.RegisterNativeAsync(channel.Uri);
This code creates the record necessary for the Notification Hub to track the device, this includes knowing when the app is removed from the device. The hubname in this case represents the literal name of the your Notification Hub has displayed in the Azure Portal:
The second parameter is the connection string for talking to the Hub. By default Azure will provide two strings, one which allows for only listening and another gives full rights to your Hub. Each of these strings can be found by accessing Connection Information from you Notification Hub Dashboard.
The final step is to configure the Notification Hub to access the WNS Push Service. To do that we need two values: the Package SID and Client Secret values, we will input both values under the Configure tab. These values are obtained from the Windows Developer Dashboard, though they are not easy to find.
First, access your Windows Developer Dashboard (https://appdev.microsoft.com/StorePortals/en-us/Home/Index). You should see your app listed in this grid
Select the option to Edit and then selected Services from the list. Now if you are like most people you might think you have reached a dead end. This is one of the more annoying parts of Windows Store development that I keep hoping Microsoft changes. There is a link on this page that will take you were you want go. I have pointed it out in the image below (Live Services site):
So, this will take you to a take where the Package SID and Client Secret are listed. Copy and paste these values into the appropriate fields on the Configure table in you Azure Notification Hub.
Let’s Test and make sure nothing is broken
So, we could go through the motions of creating an app that sends notifications to make sure this all working but, there is an easier way. It isnt a fully integrated tool yet but Microsoft provides a tool called Service Bus Explorer, currently version 2.5. (https://code.msdn.microsoft.com/windowsapps/Service-Bus-Explorer-f2abca5a). The code downloads as source, but you can find a pre-built executable in the Debug folder.
Once you have opened the application select File –> Connect. This will immediately prompt you for a connection string. You might be tempted to put in the Full connection string listed with you Notification Hub, don’t, it wont work. You need the root connection string which is listed somewhere else.
With your Notification Hub dashboard shown hit the back link in the Azure Portal (the giant back arrow inside a circle, not the browser back button). This will take you to a new screen with the title akin to your Notification Hub name suffixed with a –ns. Click on the thunderbolt icon:
From this screen click the Connection Information button from the bottom bar. This will give you the Root key. This is the value that needs to be used for Service Bus Explorer to connect.
Once connected the first thing to do is click the Registrations button. If the registration process was successful you should see at least one entry in the final listing. Finally, selecting the Notification Hub in the tree view menu will enable the Send button which allows you to send push notifications to register clients. In addition, it provides great reporting on problems and is very useful for diagnosing problems with notifications.
Hopefully that is enough to get you started. I do plan to release another entry to show how to make a client. Otherwise, this is where I got started http://azure.microsoft.com/en-us/documentation/videos/notification-hubs-broadcasting-alerts/. The big add with Notification hubs is tags which allow clients to register for certain notifications and the REST API which allows any application to send a notification. I still want to look at their integration with Azure Mobile Services since that is where I intend to focus my efforts.