Microsoft Azure - Send Push Notifications With UWP Apps - Part Two

Introduction

This article is a continuation of the Notification Hub on Microsoft Azure series. Please go through the following link to learn more about Notification Hub.

Note - You should have a Notification Hub created to work on this demo.

Technical Requirements
  1. Microsoft Azure account – click here to get an Azure account.
  2. Visual Studio 2015 installed on your laptop.

Click here to go to Part 1: Send Push Notification with UWP Apps in Microsoft Azure.

Step 17

Open Visual Studio 2015 and create a new project for UWP as Blank App in Universal. Go for "Manage NuGet Packages" in Solution Explorer, as shown below.



Step 18

Go to "Browse" and search for "WindowsAzure.Messaging.Managed" and click "Install".



Step 19

Click on "I Accept" for the license agreement to add the WindowsAzure.Messaging.Managed NuGet Packages in your solution file.



After the package is added, you will get a notification that the package has been added.



Step 20

Open App.xaml.cs in the Solution Explorer and copy the below "Using Statements" on it.

  1. using Windows.Networking.PushNotifications;  
  2. using Microsoft.WindowsAzure.Messaging;  
  3. using Windows.UI.Popups;  


Step 21

Again, in App.xaml.cs, add the below code for InitNotificationsAsync method definition to the App class.
  1. private async void InitNotificationsAsync()  
  2.  {  
  3.      var channel = await PushNotificationChannelManager.CreatePushNotificationChannelForApplicationAsync();  
  4.   
  5.      var hub = new NotificationHub("< your hub name>""<Your DefaultListenSharedAccessSignature connection string>");  
  6.      var result = await hub.RegisterNativeAsync(channel.Uri);  
  7.   
  8.      // Displays the registration ID so you know it was successful  
  9.      if (result.RegistrationId != null)  
  10.      {  
  11.          var dialog = new MessageDialog("Registration successful: " + result.RegistrationId);  
  12.          dialog.Commands.Add(new UICommand("OK"));  
  13.          await dialog.ShowAsync();  
  14.      }  
  15.  }  


Note

Replace the <your hub name> with your Notification Hub name and <Your DefaultListenSharedAccessSignatureconnectionstring> with your Notification Hub’s connection string which you can find in the Access Policies of Notification Hub account.



Step 22

Now, at the top of OnLaunched Event handler in App.xaml.cs, add the below method.
  1. InitNotificationsAsync();  
Step 23

Click on the "Local Machine" to run this app on your Visual Studio 2015.



You can find the status of the project getting build on your Visual Studio in the below pane, as shown below.



You can also find the app in your Start menu of your laptop and now the app is ready to receive toast notifications.

Step 24

Let's send some notifications using "Test Send" in the Notification Hub now.

Log into Azure portal and go for the respective Notification Hub to send notifications.



Step 25

Click on "Test Send" in the Notification Hub.



Select "Windows" in Platforms dropdown.



Select Notification Type as "Toast".



Click on "Send" now.



This will send a test message with the help of Notification Hub. You can also find the result of the notification service with the help of the result pane available there.



This will help you to send notifications to all the users who are using this app in their devices all over the world, with the help of Notification Hub on Microsoft Azure.

Keynotes in Short 
  1. Creating a Visual Studio Project on UWP.
  2. Adding NuGet packages.
  3. Sending messages from Notification Hubs on Microsoft Azure.


Similar Articles