[UWP] Sending Push Notifications from an Azure WebJob to any UWP applications using Azure Notification HubNovember 10th, 2015 | Posted by in .NET | Azure | UWP | Windows 8 | Windows Phone | WinRT | Read: 4,932
For a small side project I’m working on, I wanted to easily send push notifications to my UWP application. So I’ve decided to use the Notifications Hub (which is, in fact, a wrapper around the Microsoft Service Bus) to send the notifications.
Basically, you first need to create an application in the Store so you can access to its notifications settings:
With the “Package SID” and “Client Secret” settings copied, you can go to the Azure Portal to create/edit your Notification Hub (under the Service Bus section) and set the properties:
Now, associate your UWP application with the Store application, so the manifest file can be correctly updated with the correct values:
Your Notification Hub is up and running, you app is correctly associated so now, it’s time to add some code!
First step is to register your application so it can receive the notifications. For that, we need to get a push notification channel for the application and use it to register the app to the Notification Hub (after adding the WindowsAzure.Messaging.Managed Nuget package)
var channel = await PushNotificationChannelManager.CreatePushNotificationChannelForApplicationAsync(); var hub = new NotificationHub(Constants.NOTIFICATION_HUB_PATH, Constants.NOTIFICATION_HUB_LISTEN_ENDPOINT); await hub.RegisterNativeAsync(channel.Uri);
The notification hub path is, basically, your hub name. For the endpoint, you need to go to the Azure portal to get, from the “Dashboard” tab, the connection information:
By default, a notification hub has 2 different endpoints configured, each of them with different permissions:
As the application will only received notification, the “Listen” permission is the only one needed. So, it’s the DefaultListenSharedAccessSignature that we need to use as endpoint with the hub path.
The application is now configured, it’s time to see how to send the notifications (in my case, I’m using an Azure WebJob executing a Console app). Add the Nuget package Microsoft.Azure.NotificationHubs and you’re ready to create the NotificationHubClient object used to send the notifications:
var hub = NotificationHubClient.CreateClientFromConnectionString(Constants.NOTIFICATION_HUB_FULL_ENDPOINT, Constants.NOTIFICATION_HUB_PATH); var windowToastTemplate = "<?xml version=\"1.0\" encoding=\"utf - 8\"?>" + "<toast launch=\"fromNotification\">" + "<visual>" + "<binding template=\"ToastText01\">" + "<text id = \"1\">My super notification</text>" + "</binding>" + "</visual>" + "</toast>"; await hub.SendWindowsNativeNotificationAsync(windowToastTemplate);
Here, you can see that I’m using SendWindowsNativeNotificationASync to send the notification to my PC (running Windows 10) but, as you can see, you can send a lot of different notifications (if you have correctly configured their settings in the Azure portal):
Also, the XML payload used for the notification is the specific part of the project: feel free to use another template and, even more, to use the new template of Windows 10 if needed : https://github.com/Microsoft/Windows-universal-samples/tree/master/Samples/Notifications
Now, you just need to schedule your Azure WebJob (thanks to Visual Studio, it’s just a right-clic on your project) and define the schedule parameters: name, frequency, etc.:
Now, just wait for your WebJob to run and you should receive a nice notification (toast, tile, etc.) on your device, depending of the notification type you’ve decided to use.
Bonus tip: on your Notification Hub, you can use the “DEBUG” tab to send test notifications to your devices!
As you can see, using a Notification Hub is really easy and can be implemented in a few minutes in any kind of applications!