教學課程:使用 Azure 通知中樞將通知傳送至通用 Windows 平台應用程式Tutorial: Send notifications to Universal Windows Platform apps by using Azure Notification Hubs

在本教學課程中,您會使用 Azure 通知中樞將推播通知傳送至通用 Windows 平台 (UWP) 應用程式。In this tutorial, you create a notification hub to send push notifications to a Universal Windows Platform (UWP) app. 您會使用 Windows 推播通知服務 (WNS),建立可接收推播通知的空白 Windows 市集應用程式。You create a blank Windows Store app that receives push notifications by using the Windows Push Notification Service (WNS). 接著,使用通知中樞將推播通知廣播到所有執行您應用程式的裝置。Then, you use your notification hub to broadcast push notifications to all devices that are running your app.

注意

您可以在 GitHub 上找到本教學課程的完整程式碼。You can find the completed code for this tutorial on GitHub.

請執行下列步驟:You take the following steps:

  • 在 Windows 市集中建立應用程式Create an app in Windows Store
  • 建立通知中樞Create a Notification Hub
  • 在 Windows 應用程式中建立範例Create a sample Windows app
  • 傳送測試通知Send test notifications

必要條件Prerequisites

  • Azure 訂用帳戶Azure subscription. 如果您沒有 Azure 訂用帳戶,請在開始前建立免費 Azure 帳戶If you don't have an Azure subscription, create a free Azure account before you begin.
  • Microsoft Visual Studio Community 2015 或更新版本。Microsoft Visual Studio Community 2015 or later or later.
  • 已安裝 UWP 應用程式開發工具UWP app-development tools installed
  • 有效的 Windows 市集帳戶An active Windows Store account
  • 確認已啟用 [從應用程式和其他傳送者取得通知] 設定。Confirm that Get notifications from apps and other senders setting is enabled.
    • 在電腦上啟動 [設定] 視窗。Launch Settings window on your computer.
    • 選取 [系統] 圖格。Select the System tile.
    • 從左側功能表中選取 [通知與動作] 。Select Notifications & actions from the left menu.
    • 確認已啟用 [從應用程式和其他傳送者取得通知] 設定。Confirm that the Get notifications from apps and other senders setting is enabled. 如果未啟用,請加以啟用。If it isn't enabled, enable it.

完成本教學課程是參加 UWP 應用程式所有其他通知中樞教學課程的先決條件。Completing this tutorial is a prerequisite for all other Notification Hubs tutorials for UWP apps.

在 Windows 市集中建立應用程式Create an app in Windows Store

若要傳送推播通知給 Windows UWP 應用程式,將您的應用程式與 Windows 市集產生關聯。To send push notifications to UWP apps, associate your app to the Windows Store. 接著設定您的通知中樞,以便與 WNS 進行整合。Then, configure your notification hub to integrate with WNS.

  1. 瀏覽至 Windows 開發人員中心,並使用 Microsoft 帳戶登入,然後選取 [建立新的應用程式] 。Navigate to the Windows Dev Center, sign in with your Microsoft account, and then select Create a new app.

    新增應用程式按鈕

  2. 輸入您的應用程式名稱,然後選取 [保留產品名稱] 。Type a name for your app, and then select Reserve product name. 這麼做會為您的應用程式建立新的 Windows 市集註冊。Doing so creates a new Windows Store registration for your app.

    儲存應用程式名稱

  3. 展開 [應用程式管理] ,然後依序選取 [WNS/MPNS] 和 [線上服務網站] 。Expand App Management, select WNS/MPNS, and then select Live Services site. 登入您的 Microsoft 帳戶。Sign in to your Microsoft account. [應用程式註冊入口網站] 會在新的索引標籤中開啟。或者,您可以直接瀏覽至 應用程式註冊入口網站 並選取您的應用程式名稱,以取得此頁面。The Application Registration Portal opens in a new tab. Alternatively, you can navigate directly to the Application Registration Portal, select your application name to get to this page.

    WNS MPNS 頁面

  4. 請記下 [應用程式密碼] 和 [套件安全性識別碼] (SID) 。Note the Application Secret password and the Package security identifier (SID).

    警告

    應用程式密碼與封裝 SID 是重要的安全性認證。The application secret and package SID are important security credentials. 請勿與任何人共用這些值,或與您的應用程式一起散發密碼。Do not share these values with anyone or distribute them with your app.

建立通知中樞Create a Notification Hub

  1. 登入 Azure 入口網站Sign in to the Azure portal.

  2. 選取左功能表上的 [所有服務] ,然後選取 [行動] 區段中的 [通知中樞] 。Select All services on the left menu, and then select Notification Hubs in the 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 入口網站 - 選取通知中樞

  3. 在 [通知中樞] 頁面上,選取工具列上的 [新增] 。On the Notification Hubs page, select Add on the toolbar.

    通知中樞 - 新增工具列按鈕

  4. 在 [通知中樞] 頁面上,執行下列步驟:On the Notification Hub page, do the following steps:

    1. 在 [通知中樞] 中輸入名稱。Enter a name in Notification Hub.

    2. 在 [建立新的命名空間] 中輸入名稱。Enter a name in Create a new namespace. 命名空間包含一或多個中樞。A namespace contains one or more hubs.

    3. 從 [位置] 下拉式清單方塊選取值。Select a value from the Location drop-down list box. 此值會指定您要在其中建立中樞的位置。This value specifies the location in which you want to create the hub.

    4. 在 [資源群組] 中選取現有的資源群組,或建立一個新群組。Select an existing resource group in Resource Group, or create a name for a new resource group.

    5. 選取 [建立] 。Select Create.

      Azure 入口網站 - 設定通知中樞屬性

  5. 選取 [通知] (鈴鐺圖示),然後選取 [前往資源] 。Select Notifications (the bell icon), and then select Go to resource. 您也可以重新整理 [通知中樞] 頁面中的清單,然後選取您的中樞。You can also refresh the list on the Notification Hubs page and select your hub.

    Azure 入口網站 - 通知 -> 前往資源

  6. 從清單中選取 [存取原則] 。Select Access Policies from the list. 請記下您可使用的兩個連接字串。Note that the two connection strings are available to you. 您稍後需要用到這些連接字串來處理推播通知。You'll need them later to handle push notifications.

    重要

    「請勿」 在應用程式中使用 DefaultFullSharedAccessSignature 原則。Do not use the DefaultFullSharedAccessSignature policy in your application. 這只能在後端使用。This is meant to be used in your back end only.

    Azure 入口網站 - 通知中樞連接字串

設定中樞的 WNS 設定Configure WNS settings for the hub

  1. 在 [通知設定] 類別中選取 [Windows (WNS)] 。In the NOTIFICATION SETTINGS category, select Windows (WNS).

  2. 輸入您在上一節記下的 [套件 SID] 和 [安全性金鑰] 值。Enter values for Package SID and Security Key you noted from the previous section.

  3. 按一下工具列的 [儲存] 。Click Save on the toolbar.

    套件 SID 和安全性金鑰方塊

您的通知中樞現在已設定成使用 WNS。Your notification hub is now configured to work with WNS. 您已擁有可用來註冊應用程式和傳送通知的連接字串。You have the connection strings to register your app and send notifications.

在 Windows 應用程式中建立範例Create a sample Windows app

  1. 在 Visual Studio 中,開啟 [檔案] 功能表,選取 [新增] ,然後選取 [專案] 。In Visual Studio, open the File menu, select New, and then select Project.

  2. 在 [新增專案] 對話方塊中,完成下列步驟:In the New Project dialog, complete the following steps:

    1. 展開 Visual C#Expand Visual C#.

    2. 選取 [Windows 通用] 。Select Windows Universal.

    3. 選取 [空白應用程式] (通用 Windows) 。Select Blank App (Universal Windows).

    4. 輸入專案的 [名稱] 。Enter a name for the project.

    5. 選取 [確定] 。Select OK.

      [新增專案] 對話方塊

  3. 接受目標最小平台版本的預設值,然後選取 [確定] 。Accept the defaults for the target and minimum platform versions, and select OK.

  4. 在 [方案總管] 中,以滑鼠右鍵按一下 Windows 市集應用程式專案,然後依序選取 [市集] 和 [將應用程式與市集建立關聯] 。In Solution Explorer, right-click the Windows Store app project, select Store, and then select Associate App with the Store. 隨即顯示 [將您的應用程式與 Windows 市集建立關聯] 精靈。The Associate Your App with the Windows Store wizard appears.

  5. 在此精靈中,使用您的 Microsoft 帳戶登入。In the wizard, sign in with your Microsoft account.

  6. 選取您在步驟 2 中註冊的應用程式,選取 [下一步] ,然後選取 [關聯] 。Select the app that you registered in step 2, select Next, and then select Associate. 這麼做會將所需的 Windows 市集註冊資訊新增至應用程式資訊清單。Doing so adds the required Windows Store registration information to the application manifest.

  7. 在 Visual Studio 中,以滑鼠右鍵按一下方案,然後選取 [管理 NuGet 套件] 。In Visual Studio, right-click the solution, and then select Manage NuGet Packages. [管理 NuGet 套件] 視窗隨即開啟。The Manage NuGet Packages window opens.

  8. 在搜尋方塊中,輸入 WindowsAzure.Messaging.Managed,選取 [安裝] 並接受使用條款。In the search box, enter WindowsAzure.Messaging.Managed, select Install, and accept the terms of use.

    [管理 NuGet 套件] 視窗

    此動作會使用 Microsoft.Azure.NotificationHubs NuGet 套件來下載、安裝及新增對 Windows 適用之 Azure 通知程式庫的參考。This action downloads, installs, and adds a reference to the Azure Notification Hubs library for Windows by using the Microsoft.Azure.NotificationHubs NuGet package.

  9. 開啟 App.xaml.cs 專案檔案,並新增下列陳述式:Open the App.xaml.cs project file, and add the following statements:

    using Windows.Networking.PushNotifications;
    using Microsoft.WindowsAzure.Messaging;
    using Windows.UI.Popups;
    
  10. 在專案的 App.xaml.cs 檔案中,找出 App 類別,然後新增下列 InitNotificationsAsync 方法定義:In the project's App.xaml.cs file, locate the App class, and add the following InitNotificationsAsync method definition:

    private async void InitNotificationsAsync()
    {
        var channel = await PushNotificationChannelManager.CreatePushNotificationChannelForApplicationAsync();
    
        var hub = new NotificationHub("<your hub name>", "<Your DefaultListenSharedAccessSignature connection string>");
        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();
        }
    }
    

    此程式碼會從 WNS 中擷取應用程式的通道 URI,然後向您的通知中樞註冊該通道 URI。This code retrieves the channel URI for the app from WNS, and then registers that channel URI with your notification hub.

    注意

    使用 Azure 入口網站中顯示的通知中樞名稱,取代 hub name 預留位置。Replace the hub name placeholder with the name of the notification hub that appears in the Azure portal. 此外,使用您在上一節中從通知中樞的 [存取原則] 頁面取得的 DefaultListenSharedAccessSignature 連接字串,取代連接字串預留位置。Also replace the connection string placeholder with the DefaultListenSharedAccessSignature connection string that you obtained from the Access Polices page of your notification hub in a previous section.

  11. App.xaml.cs 中的 OnLaunched 事件處理常式頂端,將下列呼叫新增至新的 InitNotificationsAsync 方法:At the top of the OnLaunched event handler in App.xaml.cs, add the following call to the new InitNotificationsAsync method:

    InitNotificationsAsync();
    

    此動作會保證每次啟動應用程式時,通道 URI 便會在通知中樞中註冊。This action guarantees that the channel URI is registered in your notification hub each time the application launches.

  12. 按鍵盤的 F5 鍵以執行應用程式。To run the app, press the keyboard's F5 key. 包含註冊金鑰的對話方塊將會顯示。A dialog box containing the registration key will display. 按一下 [確定] 以關閉對話方塊。To close the dialog, click OK.

    註冊成功

您的應用程式現在已能夠接收快顯通知。Your app is now ready to receive toast notifications.

傳送測試通知Send test notifications

Azure 入口網站中傳送通知,即可在應用程式中快速測試通知的接收。You can quickly test receiving notifications in your app by sending notifications in the Azure portal.

  1. 在 Azure 入口網站中,切換至 [概觀] 索引標籤,然後在工具列上選取 [測試傳送] 。In the Azure portal, switch to the Overview tab, and select Test Send on the toolbar.

    測試傳送按鈕

  2. 在 [測試傳送] 視窗中,執行下列動作:In the Test Send window, do the following actions:

    1. 針對 [平台] ,選取 [Windows] 。For Platforms, select Windows.

    2. 針對 [通知類型] ,選取 [快顯通知] 。For Notification Type, select Toast.

    3. 選取 [傳送] 。Select Send.

      [傳送測試] 窗格

  3. 在視窗底部的 [結果] 清單中,檢視傳送作業的結果。See the result of the Send operation in the Result list at the bottom of the window. 您也會看到警示訊息。You also see an alert message.

    傳送作業的結果

  4. 您會看見通知訊息:在您的桌上型電腦上測試訊息You see the notification message: Test message on your desktop.

    通知訊息

後續步驟Next steps

您已使用入口網站或主控台應用程式,將廣播通知傳送至您的所有 Windows 裝置。You have sent broadcast notifications to all your Windows devices by using the portal or a console app. 若想了解如何將通知推送至特定裝置,請繼續進行下列教學課程:To learn how to push notifications to specific devices, advance to the following tutorial: