Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
291 lines (178 sloc) 16.747 kb

Getting Started with Notification Hubs

Overview

This topic shows you how to use Azure Notification Hubs to send push notifications to a Windows Store or Windows Phone 8.1 (non-Silverlight) application. If you are targeting Windows Phone 8.1 Silverlight, please refer to the Windows Phone version. In this tutorial you create a blank Windows Store app that receives push notifications using the Windows Push Notification service (WNS). When complete, you will be able to broadcast push notifications to all the devices running your app using your notification hub.

This tutorial demonstrates a simple broadcast scenario using Notification Hubs. Be sure to follow along with the next tutorial to learn how to use Notification Hubs to address specific users and groups of devices.

Prerequisites

This tutorial requires the following:

  • Microsoft Visual Studio Express 2013 for Windows with Update 2
    This version of Visual Studio is required to create a universal app project. If you just want to create a Windows Store app, you need Visual Studio 2012 Express for Windows 8.

  • An active Windows Store account

  • 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.

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

Register your app for the Windows Store

To send push notifications to Windows Store apps from Mobile Services, you must submit your app to the Windows Store. You must then configure your notification hub to integrate with WNS.

  1. If you have not already registered your app, navigate to the Submit an app page in the Dev Center for Windows Store apps, log on with your Microsoft account, and then click App name.

  2. Type a name for your app in App name, click Reserve app name, and then click Save.

    This creates a new Windows Store registration for your app.

  3. In Visual Studio, create a new Visual C# Store Apps project using the Blank App template.

  4. In Solution Explorer, right-click the Windows Store app project, click Store, and then click Associate App with the Store....

    The Associate Your App with the Windows Store wizard appears.

  5. In the wizard, click Sign in and then log in with your Microsoft account.

  6. Click the app that you registered in step 2, click Next, and then click Associate.

    This adds the required Windows Store registration information to the application manifest.

  7. (Optional) Repeat steps 4-6 for the Windows Phone Store app project.

  8. Back in the Windows Dev Center page for your new app, click Services.

  9. In the Services page, click Live Services site under Microsoft Azure Mobile Services.

  10. In the App Settings tab, make a note of the values of Client secret and Package security identifier (SID).

    [AZURE.NOTE] Security Note The client secret and package SID are important security credentials. Do not share these values with anyone or distribute them with your app.

Configure your Notification Hub

  1. Log on to the Azure Management Portal, and click NEW at the bottom of the screen.

  2. Click on App Services, then Service Bus, then Notification Hub, then Quick Create.

  3. Type a name for your notification hub, select your desired Region, and then click Create a new Notification Hub.

  4. Click the namespace you just created (usually notification hub name-ns), then click the Configure tab at the top.

  5. Select the tab Notification Hubs at the top, and then click the notification hub you just created.

  6. Select the tab Configure at the top, enter the Client secret and Package SID values you obtained from WNS in the previous section, and then click Save.

  7. Select the tab Dashboard at the top, and then click the Connection Information button at the bottom of the page. Take note of the two connection strings.

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

Connecting your app to the Notification Hub

  1. In Visual Studio, right-click the solution, then click Manage NuGet Packages.

    This displays the Manage NuGet Packages dialog box.

  2. Search for WindowsAzure.Messaging.Managed and click Install, select all projects in the solution, and accept the terms of use.

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

  3. 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;
    

    In a universal project, this file is located in the <project_name>.Shared folder.

  4. Also in App.xaml.cs, add the following InitNotificationsAsync method definition to the App class:

    private async void InitNotificationsAsync()
    {
        var channel = await PushNotificationChannelManager.CreatePushNotificationChannelForApplicationAsync();
    
        var hub = new NotificationHub("<hub name>", "<connection string with listen access>");              
        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 ChannelURI for the app from WNS, and then registers that ChannelURI with your notification hub.

    [AZURE.NOTE]Make sure to replace the "hub name" placeholder with the name of the notification hub that appears in the portal on the Notification Hubs tab (for example, mynotificationhub2 in the previous example). Also replace the connection string placeholder with the DefaultListenSharedAccessSignature connection string that you obtained in the previous section.

  5. At the top of the OnLaunched event handler in App.xaml.cs, add the following call to the new InitNotificationsAsync method:

    InitNotificationsAsync();
    

    This guarantees that the ChannelURI is registered in your notification hub each time the application is launched.

  6. In Solution Explorer double-click Package.appxmanifest of the Windows Store app, in Notifications, set Toast capable to Yes:

    From the File menu, click Save All.

  7. (Optional) Repeat the previous step in the Windows Phone Store app project.

  8. Press the F5 key to run the app. A popup dialog with the registration key is displayed.

  9. (Optional) Repeat the previous step to run the other project.

Your app is now ready to receive toast notifications.

Send notification from your back-end

You can send notifications using Notification Hubs from any back-end using the REST interface. In this tutorial you send notifications with a .NET console application. For an example of how to send notifications from an Azure Mobile Services backend integrated with Notification Hubs, see Get started with push notifications in Mobile Services (.NET backend | JavaScript backend). For an example of how to send notifications using the REST APIs, see How to use Notification Hubs from Java/PHP (Java | PHP).

  1. Right-click the solution, select Add and New Project..., then under Visual C# click Windows and Console Application and click OK.

    This adds a new Visual C# console application to the solution. You can also do this in a separate solution.

  2. In Visual Studio, click Tools, then click Nuget Package Manager, then click Package Manager Console.

    This displays the Package Manager Console in Visual Studio.

  3. In the Package Manager Console window, set the Default project to your new console application project, then in the console window execute the following command:

    Install-Package WindowsAzure.ServiceBus
    

    This adds a reference to the Azure Service Bus SDK with the WindowsAzure.ServiceBus NuGet package.

  4. Open the file Program.cs and add the following using statement:

    using Microsoft.ServiceBus.Notifications;
    
  5. In the Program class, add the following method:

    private static async void SendNotificationAsync()
    {
        NotificationHubClient hub = NotificationHubClient
            .CreateClientFromConnectionString("<connection string with full access>", "<hub name>");
        var toast = @"<toast><visual><binding template=""ToastText01""><text id=""1"">Hello from a .NET App!</text></binding></visual></toast>";
        await hub.SendWindowsNativeNotificationAsync(toast);
    }
    

    Make sure to replace the "hub name" placeholder with the name of the notification hub that appears in the portal on the Notification Hubs tab. Also, replace the connection string placeholder with the connection string called DefaultFullSharedAccessSignature that you obtained in the section "Configure your Notification Hub."

    [AZURE.NOTE]Make sure that you use the connection string with Full access, not Listen access. The listen access string does not have permissions to send notifications.

  6. Then add the following lines in the Main method:

     SendNotificationAsync();
     Console.ReadLine();
    
  7. Right click the console application project in Visual Studio and click Set as StartUp Project to set it as the startup project. Then press the F5 key to run the application.

    You will receive a toast notification on all registered devices. Clicking or taping on the toast banner loads the app.

You can find all the supported payloads in the toast catalog, tile catalog, and badge overview topics on MSDN.

Next steps

In this simple example you sent broadcast notifications to all your Windows devices. In order to target specific users, refer to the tutorial Use Notification Hubs to push notifications to users. If you want to segment your users by interest groups, see Use Notification Hubs to send breaking news. To learn more about how to use Notification Hubs, see Notification Hubs Guidance.

Jump to Line
Something went wrong with that request. Please try again.