Tutorial: Push notifications to Windows Phone apps by using Azure Notification Hubs

This tutorial shows you how to use Azure Notification Hubs to send push notifications to a Windows Phone 8 or Windows Phone 8.1 Silverlight applications. If you are targeting Windows Phone 8.1 (non-Silverlight), see the Windows Universal version of this tutorial.

In this tutorial, you create a blank Windows Phone 8 app that receives push notifications by using the Microsoft Push Notification Service (MPNS). After you create the app, you use your notification hub to broadcast push notifications to all the devices running your app.

Note

The Notification Hubs Windows Phone SDK does not support using the Windows Push Notification Service (WNS) with Windows Phone 8.1 Silverlight apps. To use WNS (instead of MPNS) with Windows Phone 8.1 Silverlight apps, follow the Notification Hubs - Windows Phone Silverlight tutorial, which uses REST APIs.

In this tutorial, you learn how to:

  • Create a notification hub
  • Create a Windows Phone application
  • Test send a notification

Prerequisites

Completing this tutorial is a prerequisite for all other Notification Hubs tutorials for Windows Phone 8 apps.

Create your notification hub

  1. Sign in to the Azure portal.

  2. Select Create a resource > 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 Windows Phone (MPNS) settings

  1. Select Windows Phone (MPNS) under NOTIFICATION SETTINGS.
  2. Select Enable authentication push.
  3. Select Save on the toolbar.

    Azure portal - Enable unauthenticated push notifications

    Your hub is now created and configured to send unauthenticated notification for Windows Phone.

    Note

    This tutorial uses MPNS in unauthenticated mode. MPNS unauthenticated mode comes with restrictions on notifications that you can send to each channel. Notification Hubs supports MPNS authenticated mode by allowing you to upload your certificate.

Create a Windows Phone application

In this section, you create a Windows Phone application that registers itself with your notification hub.

  1. In Visual Studio, create a new Windows Phone 8 application.

    Visual Studio - New Project - Windows Phone App

    In Visual Studio 2013 Update 2 or later, you instead create a Windows Phone Silverlight application.

    Visual Studio - New Project - Blank App - Windows Phone Silverlight

  2. In Visual Studio, right-click the solution, and then click Manage NuGet Packages.
  3. Search for WindowsAzure.Messaging.Managed and click Install, and then accept the terms of use.

    Visual Studio - NuGet Package Manager

  4. Open the file App.xaml.cs and add the following using statements:

     using Microsoft.Phone.Notification;
     using Microsoft.WindowsAzure.Messaging;
    
  5. Add the following code at the top of Application_Launching method in App.xaml.cs:

     private void Application_Launching(object sender, LaunchingEventArgs e)
     {
    
         var channel = HttpNotificationChannel.Find("MyPushChannel");
         if (channel == null)
         {
             channel = new HttpNotificationChannel("MyPushChannel");
             channel.Open();
             channel.BindToShellToast();
         }
    
         channel.ChannelUriUpdated += new EventHandler<NotificationChannelUriEventArgs>(async (o, args) =>
         {
             var hub = new NotificationHub("<hub name>", "<connection string>");
             var result = await hub.RegisterNativeAsync(args.ChannelUri.ToString());
    
             System.Windows.Deployment.Current.Dispatcher.BeginInvoke(() =>
             {
                 MessageBox.Show("Registration :" + result.RegistrationId, "Registered", MessageBoxButton.OK);
             });
         });
     }
    

    Note

    The value MyPushChannel is an index that is used to lookup an existing channel in the HttpNotificationChannel collection. If there isn't one there, create a new entry with that name.

    Insert the name of your hub and the connection string called DefaultListenSharedAccessSignature that you noted in the previous section. This code retrieves the channel URI for the app from MPNS, and then registers that channel URI with your notification hub. It also guarantees that the channel URI is registered in your notification hub each time the application is launched.

    Note

    This tutorial sends a toast notification to the device. When you send a tile notification, you must instead call the BindToShellTile method on the channel. To support both toast and tile notifications, call both BindToShellTile and BindToShellToast.

  6. In Solution Explorer, expand Properties, open the WMAppManifest.xml file, click the Capabilities tab, and make sure that the ID_CAP_PUSH_NOTIFICATION capability is checked. Your app can receive push notifications now.

    Visual Studio - Windows Phone App Capabilities

  7. Press the F5 key to run the app.

    A registration message is displayed in the app.

  8. Close the app or switch to the home page.

    Note

    To receive a toast push notification, the application must not be running in the foreground.

Test send a notification

  1. In the Azure portal, switch to the Overview tab.
  2. Select Test Send.

    Test Send button

  3. In the Test Send window, take the following steps:

    1. For Platforms, select Windows Phone.
    2. For Notification Type, select Toast.
    3. Select Send
    4. See the result in the list at the bottom of the window.

      Test Send window

  4. In the Windows Phone emulator or on the Windows phone, confirm that you see the notification message.

    Notification on Windows phone

Next steps

In this simple example, you broadcasted push notifications to all your Windows Phone 8 devices. Advance to the following tutorial to learn how to push notifications to specific devices: