選擇正確的推播通知通道類型Choosing the right push notification channel type

本文涵蓋三種類型的 Windows 推播通知通道 (主要、次要和替代) ,可協助您將內容傳遞至您的應用程式。This article covers the three types of Windows push notification channels (primary, secondary, and alternate) that help you deliver content to your app.

(如需有關如何建立推播通知的詳細資訊,請參閱 Windows 推播 Notification Services (WNS) 總覽。 ) (For details on how to create push notifications, see the Windows Push Notification Services (WNS) overview.)

推播通道的類型Types of push channels

有三種類型的推播通道可以用來將通知傳送至 Windows 應用程式。There are three types of push channels that can be used to send notifications to a Windows app. 其中包括:They are:

主要通道 - 「傳統」推播通道。Primary channel - the "traditional" push channel. 存放區中的任何應用程式都可以使用,以傳送快顯通知、磚、原始或徽章通知。Can be used by any app in the store to send toast, tile, raw, or badge notifications. 若要深入瞭解,請參閱。Learn more here.

次要磚頻道 -用來將磚更新推送至次要磚。Secondary tile channel - used to push tile updates to a secondary tile. 僅能用於將磚或徽章通知傳送至釘選到使用者開始畫面上的次要磚Can only be used to send tile or badge notifications to a secondary tile pinned on the user's start screen

替代通道 - Creators Update 中新增的一種通道。Alternate channel - A new type of channel added in the Creators Update. 它允許將原始通知傳送至任何 Windows 應用程式,包括未在存放區中註冊的通知。It allows for raw notifications to be sent to any Windows app, including those which aren't registered in the Store.

注意

無論您使用何種推播通道,只要您的應用程式在裝置上執行,便一律能夠傳送本機快顯通知、磚或徽章通知。No matter which push channel you use, once your app is running on the device, it will always be able to send local toast, tile, or badge notifications. 它可以從前景應用程式處理程序或從背景工作傳送本機通知。It can send local notifications from the foreground app processes or from a background task.

主要通道Primary channels

這些是 Windows 上目前最常用的通道,幾乎適用於任何要透過 Microsoft Store 散發應用程式的案例。These are the most commonly used channels on Windows right now, and are good for almost any scenario where your app is going to be distributed through the Microsoft Store. 它們允許您將所有類型的通知傳送到應用程式。They allow you to send all types of notifications to the app.

主要通道會啟用哪些項目?What do primary channels enable?

  • 將磚或徽章更新傳送至主要磚。Sending tile or badge updates to the primary tile. 如果使用者選擇將您的磚釘選在 [開始] 畫面上,這是您展示的機會。If the user has chosen to pin your tile to the start screen, this is your chance to show off. 傳送具有實用資訊的更新,或您應用程式內體驗的提醒。Send updates with useful information or reminders of experiences within your app.
  • 傳送快顯通知。Sending toast notifications. 快顯通知讓您有機會立即在使用者面前取得一些資訊。Toast notifications are a chance to get some information in front of the user immediately. 這類通知是由命令介面繪製在大多數應用程式的頂端,並存留在控制中心中,讓使用者能夠返回並在稍後與這類通知互動。They are painted by the shell over top of most apps, and live in the action center so the user can go back and interact with them later.
  • 傳送原始通知以觸發背景工作。Sending raw notifications to trigger a background task. 有時候您會想要根據通知代表使用者進行一些工作。Sometimes you want to do some work on behalf of the user based on a notification. 原始通知可讓應用程式的背景工作執行Raw notifications allow your app's background tasks to run
  • 由 Windows 使用 TLS 提供中的傳輸中訊息加密。Message encryption in transit provided by Windows using TLS. 當透過連線進入 WNS 與前往使用者裝置時加密訊息。Messages are encrypted on the wire both coming into WNS and going to the user's device.

主要通道的限制Limitations of primary channels

  • 需要使用 WNS REST API 推送通知,這不是標準的跨裝置廠商。Requires using the WNS REST API to push notifications, which isn't standard across device vendors.
  • 只能為每個應用程式建立一個通道Only one channel can be created per app
  • 您的應用程式必須在 Microsoft Store 中註冊Requires your app to be registered in the Microsoft Store

正在建立主要通道Creating a primary channel

PushNotificationChannel channel = 
    await PushNotificationChannelManager.CreatePushNotificationChannelForApplicationAsync();

次要磚通道Secondary tile channels

這些通道可用於將磚與徽章更新推送到次要磚。These are channels that can be used to push tile and badge updates to a secondary tile. 應用程式可將它們用於通知使用者有一些他們可在應用程式中互動的有趣動作或資訊,例如在群組聊天中的新訊息及最新的運動分數。These are used by apps to notify users of interesting actions or information that they can interact with in the app, such as new messages in a group chat or an updated sports score.

次要磚通道會啟用哪些項目?What do secondary tile channels enable?

  • 將磚或徽章通知傳送到次要磚。Sending tile or badge notifications to secondary tiles. 次要磚是將使用者提取回您應用程式的好方法。Secondary tiles are a great way to pull users back into your app. 這些是使用者所關注資訊的深度連結,將相關資訊放置在磚上可幫助一再將使用者帶回。They are a deep link to information they care about, and putting relevant information on the tiles helps to bring them back again and again.
  • 在各種磚之間分開通道 (並到期)。Separation of channels (and expiries) between various tiles. 這可讓您在使用者可釘選在其開始畫面上的各種類型次要磚之間將後端的邏輯分開。This allows you to separate the logic in the backend between the various types of secondary tiles that a user might pin to their start screen.
  • 由 Windows 使用 TLS 提供中的傳輸中訊息加密。Message encryption in transit provided by Windows using TLS. 當透過連線進入 WNS 與前往使用者裝置時加密訊息。Messages are encrypted on the wire both coming into WNS and going to the user's device.

次要磚通道的限制Limitations of secondary tile channels

  • 無允許的快顯通知或原始通知。No toast or raw notifications allowed. 系統會忽略傳送給次要磚的快顯通知或原始通知。Toast or raw notifications sent to a secondary tile are ignored by the system.
  • 您的應用程式必須在 Microsoft Store 中註冊Requires your app to be registered in the Microsoft Store

建立次要磚通道Creating a secondary tile channel

PushNotificationChannel channel = 
    await PushNotificationChannelManager.CreatePushNotificationChannelForSecondaryTileAsync(tileId);

替代通道Alternate channels

替代通道可讓應用程式無須向 Microsoft Store 註冊或建立應用程式所使用主要通道之外的推播通道,即可傳送推播通知。Alternate channels enable apps to send push notifications without registering to the Microsoft Store or creating push channels outside of the primary one used for the app.

替代通道會啟用哪些項目?What do alternate channels enable?

  • 將原始推播通知傳送到在任何 Windows 裝置上執行的 Windows。Send raw push notifications to a Windows running on any Windows device. 替代通道只允許原始通知 (不過,您仍然可以喚醒背景工作,以在本機顯示快顯通知或磚通知) 。Alternate channels only allow for raw notifications (however you can still wake up a background task to locally show toast or tile notifications).
  • 允許應用程式為應用程式內的不同功能建立多個原始推送通道。Allows apps to create multiple raw push channels for different features within the app. 應用程式可以建立最多 1000 個替代通道,每個通道的有效期間為 30 天。An app can create up to 1000 alternate channels, and each one is valid for 30 days. 這些通道每一個都能由應用程式分開管理或撤銷。Each of these channels can be managed or revoked separately by the app.
  • 無須向 Microsoft Store 註冊應用程式,便可建立替代推播通道。Alternate push channels can be created without registering an app with the Microsoft Store. 如果您的應用程式即將安裝在裝置上,但未將它向 Microsoft Store 註冊,則該應用程式仍能夠接收推播通知。If you app is going to be installed on devices without registering it in the Microsoft Store, it will still be able to receive push notifications.
  • 伺服器可使用 W3C 標準 REST API 與 VAPID 通訊協定推播通知。Servers can push notifications using the W3C standard REST APIs and VAPID protocol. 替代通道會使用 W3C 標準通訊協定,這可讓您簡化必須維護的伺服器邏輯。Alternate channels use the W3C standard protocol, this allows you to simplify the server logic that needs to be maintained.
  • 完整、端對端、訊息加密。Full, end-to-end, message encryption. 當主要通道一邊傳輸,一邊提供加密時,如果您想要有額外的安全性,則替代通道可讓應用程式傳遞加密標頭以保護訊息。While the primary channel provides encryption while in transit, if you want to be extra secure, alternate channels enable your app to pass through encryption headers to protect a message.

替代通道的限制Limitations of alternate channels

  • 您應用程式的伺服器無法傳送推播快顯通知、磚或徽章類型通知。Your app's server cannot send push toast, tile, or badge type notifications. 您只能傳送推播原始通知。You can only send push raw notifications. 您的應用程式仍能從您的背景工作傳送本機通知。Your app is still able to send local notifications from your background task.
  • 需要與主要和次要磚通道不同的 REST API。Requires a different REST API than either primary or secondary tile channels. 使用標準 W3C REST API,表示您的應用程式需要有不同的邏輯,才能傳送推播快顯通知或磚更新Using the standard W3C REST API means that your app will need to have different logic for sending push toast or tile updates

建立替代通道Creating an alternate channel

PushNotificationChannel webChannel = 
    await PushNotificationChannelManager.GetDefault().CreateRawPushNotificationChannelWithAlternateKeyForApplicationAsync(applicationServerKey, appChannelId);

通道類型比較Channel type comparison

以下是不同類型通道之間的快速比較:Here is a quick comparison between the different types of channels:

型別Type 推播快顯通知?Push toast? 推播磚/徽章?Push tile/badge? 推播原始通知?Push raw notifications? 驗證Authentication APIAPI 需要市集註冊?Store registration required? 聲道Channels 加密Encryption
主要Primary Yes 是 - 僅主要磚Yes - primary tile only Yes OAuthOAuth WNS REST APIWNS REST API Yes 每個應用程式一個One per app 傳輸中In Transit
次要磚Secondary Tile No 是 - 僅次要磚Yes - secondary tile only No OAuthOAuth WNS REST APIWNS REST API Yes 每個次要磚一個One per secondary tile 傳輸中In Transit
替代Alternate No No Yes VAPIDVAPID WebPush W3C 標準WebPush W3C Standard No 每個應用程式 1,000 佪1,000 per app 標頭傳遞時可採用傳輸中 + 端對端加密 (需要應用程式程式碼)In transit + end to end encryption possible with header pass through (requires app code)

選擇正確通道Choosing the right channel

一般而言,我們建議在您的應用程式中使用主要通道,但有一些例外:In general, we recommend using the primary channel in your app, with a few exceptions:

  1. 如果您將磚更新推播至次要磚,請使用次要磚推播通道。If you are pushing a tile update to a secondary tile, use the secondary tile push channel.
  2. 如果您正將通道傳出至其他服務 (例如瀏覽器),請使用替代通道。If you are passing out channels to other services (such as in the case of a browser) use the alternate channel.
  3. 如果您正建立一個將不會列在 Windows 市集中的應用程式 (例如 LOB 應用程式),請使用替代通道。If you are creating an app that won't be listed in the Windows store (such as an LOB app) use an alternate channel.
  4. 如果您的伺服器上有您想要重複使用的現有 Web 推播程式碼,或您必須在後端服務中有多個通道,請使用替代通道。If you have existing web push code on your server you wish to reuse or have a need for multiple channels in your backend service, use alternate channels.