教程:使用 Azure 通知中心向 Windows Phone 应用推送通知Tutorial: Push notifications to Windows Phone apps by using Azure Notification Hubs

本教程演示如何使用 Azure 通知中心将推送通知发送到 Windows Phone 8 或 Windows Phone 8.1 Silverlight 应用程序。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. 如果要以 Windows Phone 8.1(非 Silverlight)为目标,请参阅本教程的 Windows Universal 版本。If you are targeting Windows Phone 8.1 (non-Silverlight), see the Windows Universal version of this tutorial.

在本教程中,你将创建一个空白 Windows Phone 8 应用,它使用 Microsoft 推送通知服务 (MPNS) 接收推送通知。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

通知中心 Windows Phone SDK 不支持将 Windows 推送通知服务 (WNS) 与 Windows Phone 8.1 Silverlight 应用配合使用。The Notification Hubs Windows Phone SDK does not support using the Windows Push Notification Service (WNS) with Windows Phone 8.1 Silverlight apps. 若要将 WNS(而不是 MPNS)与 Windows Phone 8.1 Silverlight 应用配合使用,请遵循使用 REST API 的 通知中心 - Windows Phone Silverlight 教程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
  • 创建 Windows Phone 应用程序Create a Windows Phone application
  • 测试性发送通知Test send a notification

先决条件Prerequisites

完成本教程是学习有关 Windows Phone 8 应用的所有其他通知中心教程的先决条件。Completing this tutorial is a prerequisite for all other Notification Hubs tutorials for Windows Phone 8 apps.

创建通知中心Create your notification hub

  1. 登录到 Azure 门户Sign in to the Azure portal.

  2. 在左侧菜单上选择“所有服务”,然后在“移动”部分中选择“通知中心”。Select All services on the left menu, and select Notification Hubs in the Mobile section. 选择服务名称旁边的星星 (*) 以将其添加到左侧菜单上的“收藏夹”部分。Select star (*) next to the service name to add it to the FAVORITES section on the left menu. 在将“通知中心”添加到“收藏夹”之后,在菜单上选择它。After Notification Hubs is added to FAVORITES, select it on the left menu.

    Azure 门户 - 选择“通知中心”

  3. 在“通知中心”页面上,在工具栏上选择“添加”。On the Notification Hubs page, select Add on the toolbar.

    通知中心 -“添加”工具栏按钮

  4. 在“通知中心”页面上,执行以下步骤:On the Notification Hub page, do the following steps:

    1. 为通知中心指定一个名称。Specify a name for the notification hub.

    2. 为命名空间指定一个名称。Specify a name for the namespace. 命名空间包含一个或多个中心。A namespace contains one or more hubs.

    3. 选择要在其中创建通知中心的位置。Select a location in which you want to create the notification hub.

    4. 选择一个现有资源组,或者为新资源组输入一个名称。Select an existing resource group or enter a name for the new resource group.

    5. 选择“创建” 。Select Create.

      Azure 门户 - 设置通知中心属性

  5. 选择“通知”(钟形图标),然后选择“转到资源”。Select Notifications (Bell icon), and select Go to resource. 还可以刷新“通知中心”页中的列表,然后选择通知中心。You can also refresh the list in the Notification Hubs page, and select your notification hub.

    Azure 门户 - 通知 -> 转到资源

  6. 从列表中选择“访问策略”。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

    在应用程序中使用 DefaultFullSharedAccessSignature。Do NOT use the DefaultFullSharedAccessSignature in your application. 这只能在后端使用。This is meant to be used in your back-end only.

    Azure 门户 - 通知中心连接字符串

配置 Windows Phone (MPNS) 设置Configure Windows Phone (MPNS) settings

  1. 在“通知设置”下选择“Windows Phone (MPNS)”。Select Windows Phone (MPNS) under NOTIFICATION SETTINGS.

  2. 选择“启用身份验证推送”。Select Enable authentication push.

  3. 在工具栏上选择“保存”。Select Save on the toolbar.

    Azure 门户 - 启用未经身份验证的推送通知

    中心现已创建,并已配置为向 Windows Phone 发送未经身份验证的通知。Your hub is now created and configured to send unauthenticated notification for Windows Phone.

    Note

    本教程使用未经身份验证模式下的 MPNS。This tutorial uses MPNS in unauthenticated mode. MPNS 未经身份验证的模式对可以发送到每个通道的通知有一些限制。MPNS unauthenticated mode comes with restrictions on notifications that you can send to each channel. 通知中心支持 MPNS 身份验证模式,它允许上传证书。Notification Hubs supports MPNS authenticated mode by allowing you to upload your certificate.

创建 Windows Phone 应用程序Create a Windows Phone application

在此部分,请创建一个可以自行注册到通知中心的 Windows Phone 应用程序。In this section, you create a Windows Phone application that registers itself with your notification hub.

  1. 在 Visual Studio 中创建一个新的 Windows Phone 8 应用程序。In Visual Studio, create a new Windows Phone 8 application.

    Visual Studio - 新建项目 - Windows Phone 应用

    在 Visual Studio 2013 Update 2 或更高版本中,必须改为创建 Windows Phone Silverlight 应用程序。In Visual Studio 2013 Update 2 or later, you instead create a Windows Phone Silverlight application.

    Visual Studio - 新建项目 - 空白应用 - Windows Phone Silverlight

  2. 在 Visual Studio 中,右键单击该解决方案,然后单击“管理 NuGet 包”。In Visual Studio, right-click the solution, and then click Manage NuGet Packages.

  3. 搜索 WindowsAzure.Messaging.Managed,单击“安装”,然后接受使用条款。Search for WindowsAzure.Messaging.Managed and click Install, and then accept the terms of use.

    Visual Studio - NuGet 包管理器

  4. 打开文件 App.xaml.cs 并添加以下 using 语句:Open the file App.xaml.cs and add the following using statements:

     using Microsoft.Phone.Notification;
     using Microsoft.WindowsAzure.Messaging;
    
  5. App.xaml.cs 中的 Application_Launching 方法顶部添加以下代码: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

    MyPushChannel 是用于查找 HttpNotificationChannel 集合中现有通道的索引。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.

    插入中心名称以及在前一部分记下的名为 DefaultListenSharedAccessSignature 的连接字符串。Insert the name of your hub and the connection string called DefaultListenSharedAccessSignature that you noted in the previous section. 此代码从 MPNS 检索应用的通道 URI,并将该通道 URI 注册到用户的通知中心。This code retrieves the channel URI for the app from MPNS, and then registers that channel URI with your notification hub. 它还保证每次启动应用程序时都在通知中心注册通道 URI。It also guarantees that the channel URI is registered in your notification hub each time the application is launched.

    Note

    本教程将一个 toast 通知发送到设备。This tutorial sends a toast notification to the device. 发送磁贴通知时,必须在通道上调用 BindToShellTile 方法。When you send a tile notification, you must instead call the BindToShellTile method on the channel. 若要同时支持 toast 通知和磁贴通知,请同时调用 BindToShellTileBindToShellToastTo support both toast and tile notifications, call both BindToShellTile and BindToShellToast.

  6. 在解决方案资源管理器中,展开“属性”,打开 WMAppManifest.xml 文件,单击“功能”选项卡并确保选中 ID_CAP_PUSH_NOTIFICATION 功能。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 应用功能

  7. F5 键以运行应用。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

    若要接收 toast 推送通知,则应用程序不得在前台运行。To receive a toast push notification, the application must not be running in the foreground.

测试性发送通知Test send a notification

  1. 在 Azure 门户中切换到“概览”选项卡。In the Azure portal, switch to the Overview tab.

  2. 选择“测试性发送”。Select Test Send.

    “测试性发送”按钮

  3. 在“测试性发送”窗口中执行以下步骤:In the Test Send window, take the following steps:

    1. 对于“平台”,请选择“Windows Phone”。For Platforms, select Windows Phone.

    2. 对于“通知类型”,请选择“Toast”。For Notification Type, select Toast.

    3. 选择“发送”Select Send

    4. 请在窗口底部的列表中查看结果See the result in the list at the bottom of the window.

      “测试性发送”窗口

  4. 在 Windows Phone 模拟器或 Windows Phone 中,确认看到通知消息。In the Windows Phone emulator or on the Windows phone, confirm that you see the notification message.

    Windows Phone 中的通知

后续步骤Next steps

在这个简单的示例中,已将推送通知广播到所有 Windows Phone 8 设备。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: