选择正确的推送通知通道类型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. 应用商店中的任何应用均可使用来发送 toast、磁贴、原始或徽章通知。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

备用通道 - 在创意者更新中添加的一种新型通道。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.

备注

无论你使用哪种推送通道,在设备上运行你的应用后,应用将始终能够发送本地 Toast、磁贴或锁屏提醒通知。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.
  • 发送 Toast 通知。Sending toast notifications. 利用 Toast 通知,可以将一些信息立即呈现在用户面前。Toast notifications are a chance to get some information in front of the user immediately. 这些通知通过 shell 显示在大多数应用的顶部,并且位于操作中心,以便用户以后可以返回去与之进行交互。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

  • 不允许发送 Toast 或原始通知。No toast or raw notifications allowed. 系统会忽略发送到辅助磁贴的 toast 或原始通知。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. 备用通道仅允许原始通知 (但是,你仍可以将后台任务唤醒以在本地显示 toast 或磁贴通知) 。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

  • 应用的服务器无法发送推送 toast、磁贴或徽章类型通知。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 不同于主要或辅助磁贴通道所需的 REST API。Requires a different REST API than either primary or secondary tile channels. 使用标准 W3C REST API 意味着你的应用将需要使用不同的逻辑来发送推送 Toast 或磁贴更新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 推送 Toast?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 每个应用 1000 个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. 如果要创建的应用(例如 LOB 应用)将不会在 Windows 应用商店中列出,请使用备用通道。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.