路由和标记表达式Routing and tag expressions

概述Overview

通过通知中心发送推送通知时,使用标记表达式可以定向到特定的设备集或更具体的注册。Tag expressions enable you to target specific sets of devices, or more specifically registrations, when sending a push notification through Notification Hubs.

定向到特定注册Targeting specific registrations

定向到特定注册的唯一方法是将这些注册与标记相关联,并定向到这些标记。The only way to target specific notification registrations is to associate tags with them, then target those tags. 注册管理中所述,若要接收推送通知,应用必须在通知中心内注册设备句柄。As discussed in Registration Management, to receive push notifications, an app must register a device handle on a notification hub. 应用在通知中心内创建注册后,应用程序后端便可向它发送推送通知。Once the app creates a registration on a notification hub, the application backend can send push notifications to it. 应用程序后端可以通过以下方式选择要将特定通知定向到的注册:The application backend can choose the registrations to target with a specific notification in the following ways:

  1. 广播:通知中心内的所有注册均会收到通知。Broadcast: all registrations in the notification hub receive the notification.
  2. 标记:包含指定标记的所有注册会收到通知。Tag: all registrations that contain the specified tag receive the notification.
  3. 标记表达式:其标记集匹配指定表达式的所有注册将收到通知。Tag expression: all registrations whose set of tags match the specified expression receive the notification.

TagsTags

标记可以是任意字符串,最多 120 个字符,可以包含字母数字和以下非字母数字字符:“_”、“@”、“#”、“.”、“:”、“-”。A tag can be any string, up to 120 characters, containing alphanumeric and the following non-alphanumeric characters: ‘_’, ‘@’, ‘#’, ‘.’, ‘:’, ‘-’. 以下示例显示了一个应用程序,可以从中接收有关特定乐队的 toast 通知。The following example shows an application from which you can receive toast notifications about specific music groups. 在此方案中,路由通知的一种简便方法是使用代表不同乐队的标记为注册添加标签,如下图所示:In this scenario, a simple way to route notifications is to label registrations with tags that represent the different bands, as in the following figure:

标记概述

在此图中,标记为 Beatles 的消息仅传送到使用标记 Beatles 注册的平板电脑。In the figure, the message tagged with Beatles reaches only the tablet that registered with the tag Beatles.

有关针对标记创建注册的详细信息,请参阅注册管理For more information about creating registrations for tags, see Registration Management.

可使用 Azure 通知中心 SDK 中 Microsoft.Azure.NotificationHubs.NotificationHubClient 类的发送通知方法将通知发送到标记。You can send notifications to tags using the send notifications methods of the Microsoft.Azure.NotificationHubs.NotificationHubClient class in the Azure Notification Hubs SDK. 还可以使用 Node.js 或推送通知 REST API。You can also use Node.js, or the Push Notifications REST APIs. 下面是使用 SDK 的示例。Here's an example using the SDK.

Microsoft.Azure.NotificationHubs.NotificationOutcome outcome = null;

// Windows 8.1 / Windows Phone 8.1
var toast = @"<toast><visual><binding template=""ToastText01""><text id=""1"">" +
"You requested a Beatles notification</text></binding></visual></toast>";
outcome = await Notifications.Instance.Hub.SendWindowsNativeNotificationAsync(toast, "Beatles");

// Windows 10
toast = @"<toast><visual><binding template=""ToastGeneric""><text id=""1"">" +
"You requested a Wailers notification</text></binding></visual></toast>";
outcome = await Notifications.Instance.Hub.SendWindowsNativeNotificationAsync(toast, "Wailers");

不得预先预配标记,标记可以参考多个特定于应用的概念。Tags must not be pre-provisioned, and can refer to multiple app-specific concepts. 例如,此示例应用程序的用户可以对乐队发表评论,并且不仅想要接收有关其喜爱乐队的评论的 toast,而且想要接收来自其好友的所有评论(不管他们在对哪个乐队发表评论)的 toast。For example, users of this example application can comment on bands and want to receive toasts, not only for the comments on their favorite bands, but also for all comments from their friends, regardless of the band on which they are commenting. 下图突出显示了此方案的示例:The following figure highlights an example of this scenario:

标记好友

在此示例中,Alice 对于 Beatles 的更新感兴趣,而 Bob 对于 Wailers 的更新感兴趣。In this example, Alice is interested in updates for the Beatles, and Bob is interested in updates for the Wailers. Bob 还对 Charlie 的评论感兴趣,而 Charlie 对 Wailers 感兴趣。Bob is also interested in Charlie’s comments, and Charlie is interested in the Wailers. 当发送有关 Charlie 对 Beatles 的评论的通知时,通知中心会将通知发送到 Alice 和 Bob。When a notification is sent for Charlie’s comment on the Beatles, Notification Hubs sends it to both Alice and Bob.

虽然可在标记中对多个关注进行编码(例如,band_Beatlesfollows_Charlie),但标记只是简单的字符串,并不是具有值的属性。While you can encode multiple concerns in tags (for example, band_Beatles or follows_Charlie), tags are simple strings and not properties with values. 只有存在或缺少特定标记时,才会匹配某一注册。A registration matches only on the presence or absence of a specific tag.

有关如何使用标记发送到相关组的完整分步教程,请参阅突发新闻For a full step-by-step tutorial on how to use tags for sending to interest groups, see Breaking News.

备注

Azure 通知中心支持每次注册最多 60 个标记。Azure Notification Hubs supports a maximum of 60 tags per registration.

使用标记定向到用户Using tags to target users

使用标记的另一种方法就是标识与特定用户关联的所有设备。Another way to use tags is to identify all the devices associated with a particular user. 可以使用包含用户 ID 的标记来标记注册,如下图所示:You can tag a Registration with a tag that contains the user ID, as in the following figure:

标记用户

在此图中,标记为 user_Alice 的消息将抵达标记为 user_Alice 的所有设备。In the figure, the message tagged user_Alice reaches all devices tagged with user_Alice.

标记表达式Tag expressions

存在这样的情况:通知必须定向到一个注册集,该注册集不由单个标记标识,而是由使用标记的布尔表达式进行标识。There are cases where notifications must target a set of registrations identified not by a single tag, but by a Boolean expression using tags.

考虑这样一个体育应用程序,它会向波士顿的所有用户发送有关红袜队和红雀队之间的比赛的提醒。Consider a sports application that sends a reminder to everyone in Boston about a game between the Red Sox and Cardinals. 如果客户端应用注册了有关感兴趣的球队和位置的标记,则通知应定向到波士顿中对红袜队或红雀队感兴趣的所有用户。If the client app registers tags about interest in teams and location, then the notification should be targeted to everyone in Boston who is interested in either the Red Sox or the Cardinals. 此条件可以用以下布尔表达式表示:This condition can be expressed with the following Boolean expression:

(follows_RedSox || follows_Cardinals) && location_Boston

标记表达式

标记表达式支持常用的布尔运算符,例如 AND (&&)、OR (||) 和 NOT (!);它们还可以包含括号。Tag expressions support common Boolean operators such as AND (&&), OR (||), and NOT (!); they can also contain parentheses. 仅使用 OR 运算符的标记表达式可以引用 20 个标记;否则标记表达式限制为 6 个标记。Tag expressions using only OR operators can reference 20 tags; otherwise tag expressions are limited to 6 tags.

下面是通过 SDK 使用标记表达式发送通知的示例:Here's an example for sending notifications with tag expressions using the SDK:

Microsoft.Azure.NotificationHubs.NotificationOutcome outcome = null;

String userTag = "(location_Boston && !follows_Cardinals)";

// Windows 8.1 / Windows Phone 8.1
var toast = @"<toast><visual><binding template=""ToastText01""><text id=""1"">" +
"You want info on the Red Sox</text></binding></visual></toast>";
outcome = await Notifications.Instance.Hub.SendWindowsNativeNotificationAsync(toast, userTag);

// Windows 10
toast = @"<toast><visual><binding template=""ToastGeneric""><text id=""1"">" +
"You want info on the Red Sox</text></binding></visual></toast>";
outcome = await Notifications.Instance.Hub.SendWindowsNativeNotificationAsync(toast, userTag);