Tutorial: Send notifications to Universal Windows Platform apps by using Azure Notification Hubs

In this tutorial, you create a notification hub to send push notifications to a Universal Windows Platform (UWP) app. You create a blank Windows Store app that receives push notifications by using the Windows Push Notification Service (WNS). Then, you can use your notification hub to broadcast push notifications to all devices that are running your app.

Note

You can find the completed code for this tutorial on GitHub.

In this tutorial, you take the following steps:

  • Create an app in Windows Store
  • Create a notificaiton hub
  • Create a sample Windows app
  • Send test notifications

Prerequisites

Completing this tutorial is a prerequisite for all other Notification Hubs tutorials for UWP apps.

Create an app in Windows Store

To send push notifications to UWP apps, associate your app to the Windows Store. Then, configure your notification hub to integrate with WNS.

  1. Navigate to the Windows Dev Center, sign in with your Microsoft account, and then select Create a new app.

    New app button

  2. Type a name for your app, and then select Reserve product name. Doing so creates a new Windows Store registration for your app.

    Store app name

  3. Expand App Management, select WNS/MPNS, select WNS/MPNS, and then select Live Services site. Sign in to your Microsoft account. The Application Registration Portal opens in a new tab. Alternatively, you can navigate directly to the Application Registration Portal, select your application name to get to this page.

    WNS MPNS page

  4. Note the Application Secret password and the Package security identifier (SID).

    Warning

    The application secret and package SID are important security credentials. Do not share these values with anyone or distribute them with your app.

Create a notification hub

  1. Sign in to the Azure portal.

  2. Select Create a resource > Web + Mobile > Notification Hub.

    Azure portal - create a notification hub

  3. In the Notification Hub box, type a unique name. Select your Region, Subscription, and Resource Group (if you have one already).

    If you don't already have a service bus namespace, you can use the default name, which is created based on the hub name (if the namespace name is available).

    If you already have a service bus namespace that you want to create the hub in, follow these steps

    a. In the Namespace area, select the Select Existing link.

    b. Select Create.

    Azure portal - set notification hub properties

  4. Select Notifications (Bell icon), and select Go to resource.

    Azure portal - notifications -> Go to resource

  5. Select Access Policies from the list. Note the two connection strings that are available to you. You need them to handle push notifications later.

    Important

    Do NOT use the DefaultFullSharedAccessSignature in your application. This is meant to be used in your back-end only.

    Azure portal - notification hub connection strings

Configure WNS settings for the hub

  1. Select Windows (WNS) in the NOTIFICATION SETTINGS category.
  2. Enter values for Package SID and Security Key you noted from the previous section.
  3. Select Save on the toolbar.

    The Package SID and Security Key boxes

Your notification hub is now configured to work with WNS. You have the connection strings to register your app and send notifications.

Create a sample Windows app

  1. In Visual Studio, select File, point to New, and select Project.
  2. In the New Project dialog box, do the following steps:

    1. Expand Visual C#.
    2. Select Windows Universal.
    3. Select Blank App (Universal Windows).
    4. Enter a name for the project.
    5. Select OK.

      New Project dialog

  3. Accept the defaults for the target and minimum platform versions, and select OK.
  4. In Solution Explorer, right-click the Windows Store app project, select Store, and then select Associate App with the Store. The Associate Your App with the Windows Store wizard appears.
  5. In the wizard, sign in with your Microsoft account.
  6. Select the app that you registered in step 2, select Next, and then select Associate. Doing so adds the required Windows Store registration information to the application manifest.
  7. In Visual Studio, right-click the solution, and then select Manage NuGet Packages. The Manage NuGet Packages window opens.
  8. In the search box, enter WindowsAzure.Messaging.Managed, select Install, and accept the terms of use.

    The Manage NuGet Packages window

    This action downloads, installs, and adds a reference to the Azure Notification Hubs library for Windows by using the Microsoft.Azure.NotificationHubs NuGet package.

  9. Open the App.xaml.cs project file, and add the following using statements:

     using Windows.Networking.PushNotifications;
     using Microsoft.WindowsAzure.Messaging;
     using Windows.UI.Popups;
    
  10. In App.xaml.cs, also add to the App class the following InitNotificationsAsync method definition:

     private async void InitNotificationsAsync()
     {
         var channel = await PushNotificationChannelManager.CreatePushNotificationChannelForApplicationAsync();
    
         var hub = new NotificationHub("<your hub name>", "<Your DefaultListenSharedAccessSignature connection string>");
         var result = await hub.RegisterNativeAsync(channel.Uri);
    
         // Displays the registration ID so you know it was successful
         if (result.RegistrationId != null)
         {
             var dialog = new MessageDialog("Registration successful: " + result.RegistrationId);
             dialog.Commands.Add(new UICommand("OK"));
             await dialog.ShowAsync();
         }
    
     }
    

    This code retrieves the channel URI for the app from WNS, and then registers that channel URI with your notification hub.

    Note

    • Replace the hub name placeholder with the name of the notification hub that appears in the Azure portal.
    • Also replace the connection string placeholder with the DefaultListenSharedAccessSignature connection string that you obtained from the Access Polices page of your notification hub in a previous section.
  11. At the top of the OnLaunched event handler in App.xaml.cs, add the following call to the new InitNotificationsAsync method:

     InitNotificationsAsync();
    

    This action guarantees that the channel URI is registered in your notification hub each time the application is launched.

  12. To run the app, select the F5 key. A dialog box that contains the registration key is displayed. To close the dialog, select OK.

    Registration successful

Your app is now ready to receive toast notifications.

Send test notifications

You can quickly test receiving notifications in your app by sending notifications in the Azure portal.

  1. In the Azure portal, switch to the Overview tab, and select Test Send on the toolbar.

    Test Send button

  2. In the Test Send window, do the following actions:

    1. For Platforms, select Windows.
    2. For Notification Type, select Toast.
    3. Select Send.

      The Test Send pane

  3. See the result of the Send operation in the Result list at the bottom of the window. You also see an alert message.

    Result of Send operation

  4. You see the notification message: Test message on your desktop.

    Notification message

Next steps

In this tutorial, you sent broadcast notifications to all your Windows devices by using the portal or a console app. To learn how to push notifications to specific devices, advance to the following tutorial: