Get started with Azure Notification Hubs for Windows Phone apps

Overview

Note

To complete this tutorial, you must have an active Azure account. If you don't have an account, you can create a free trial account in just a couple of minutes. For details, see Azure Free Trial.

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 application. If you are targeting Windows Phone 8.1 (non-Silverlight), then refer to the Windows Universal version. In this tutorial, you create a blank Windows Phone 8 app that receives push notifications by using the Microsoft Push Notification Service (MPNS). When you're finished, you'll be able to 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.

The tutorial demonstrates the simple broadcast scenario in using Notification Hubs.

Prerequisites

This tutorial requires the following:

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 > 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. After you've created the namespace and notification hub, open it by selecting All resources and then select the created notification hub from the list.

    Azure portal - notification hub portal page

  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

  1. Under Notification Services, select Windows Phone (MPNS) and then click the Enable unauthenticated push check box.

  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.

Connecting your app to the 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.

    This displays the Manage NuGet Packages dialog box.

  3. Search for WindowsAzure.Messaging.Managed and click Install, and then accept the terms of use.

    Visual Studio - NuGet Package Manager

    This downloads, installs, and adds a reference to the Azure Messaging library for Windows by using the WindowsAzure.Messaging.Managed NuGet package.

  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:

     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.

    Make sure to insert the name of your hub and the connection string called DefaultListenSharedAccessSignature that you obtained 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.

    Visual Studio - Windows Phone App Capabilities

    This ensures that your app can receive push notifications. Without it, any attempt to send a push notification to the app will fail.

  7. Press the F5 key to run the app.

    A registration message is displayed in the app.

  8. Close the app.

    Note

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

Next steps

In this simple example, you broadcasted push notifications to all your Windows Phone 8 devices.

In order to target specific users, refer to the Use Notification Hubs to push notifications to users tutorial.

If you want to segment your users by interest groups, you can read Use Notification Hubs to send breaking news.

Learn more about how to use Notification Hubs in Notification Hubs Guidance.