Tutorial: Send push notifications to Windows Phone apps using Notification Hubs

Note

Azure Push Notification Service (MPNS) has been deprecated and is no longer supported.

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 Azure 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 All services on the left menu, and then select Notification Hubs in the Web + Mobile section. Select the star icon next to the service name to add the service to the FAVORITES section on the left menu. After you add Notification Hubs to FAVORITES, select it on the left menu.

    Azure portal - select Notification Hubs

  3. On the Notification Hubs page, select Create on the toolbar.

    Notification Hubs - Add toolbar button

  4. In the Basics tab on the Notification Hub page, do the following steps:

    1. In Subscription, select the name of the Azure subscription you want to use, and then select an existing resource group, or create a new one.

    2. Enter a unique name for the new namespace in Namespace Details.

    3. A namespace contains one or more notification hubs, so type a name for the hub in Notification Hub Details.

    4. Select a value from the Location drop-down list box. This value specifies the location in which you want to create the hub.

    5. Select Create.

  5. When the deployment is complete select Go to resource.

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: