【问题标题】:How do you send a notification to a specific Notification Hub installation template?如何将通知发送到特定的通知中心安装模板?
【发布时间】:2017-10-27 14:06:58
【问题描述】:

我正在从我的 .NET 后端服务器代码注册安装,每个都有多个模板。例如:

var installation = new Installation
{
    InstallationId = id,
    PushChannel = token,
    Templates = new Dictionary<string, InstallationTemplate>(),
    Tags = new List<string> { "userId:123456" },
    Platform = NotificationPlatform.Gcm
};

installation.Templates.Add("template1", new InstallationTemplate { Body = "{\"data\":{\"message\":\"$(message)\"}}"});
installation.Templates.Add("template2", new InstallationTemplate { Body = "{\"data\":{\"message2\":\"$(message)\"}}"});

await _client.CreateOrUpdateInstallationAsync(installation);

发送通知时如何定位特定模板?我在 SDK 中看到的内容如下:

await _client.SendTemplateNotificationAsync(
    new Dictionary<string, string>
    {
        { "message",  "Hello world." }
    }, "userId:123456");

SendTemplateNotificationAsync 方法没有任何参数可以让我指定目标模板(例如,template2)。

将使用哪个模板?我是不是误会了什么?

【问题讨论】:

    标签: c# .net azure azure-notificationhub


    【解决方案1】:

    InstallationTemplate class 具有 Tags 属性。这是区分模板的一种方式。

    在您的情况下,您似乎可以跳过通过Installation.Tags 属性标记整个安装,并通过InstallationTemplate.Tags 在特定模板上使用类似userId:123456-template 的标记。然后以与您相同的方式调用SendTemplateNotificationAsync,但使用模板后缀。

    【讨论】:

    • 所以特定模板是通过SendTemplateNotificationAsync 上的tagExpression 定位的?用上面的例子,我可以做一个tagExpression(userId:123456 &amp;&amp; template2) 吗?多个用户可以拥有相同的模板 ID 吗?例如,如果我为另一个用户进行了相同的安装,userId:789 并向他们发送相同类型的通知tagExpression(userId:789 &amp;&amp; template2)
    • 我认为你可以,但是与发送到单个标签相比,表达式评估使它变慢。因此,如果您可以以独特的方式标记单个模板(从而避免模板表达式),那么它会工作得更快。
    • 如果我只针对单个用户,创建您所描述的标记后缀是可行的,但是,当我想使用相同的模板发送给多个用户时,我有一些用例。例如,多个用户有一个departmentId:123 标签,我想向他们发送所有相同的消息(模板)。我猜这种情况的标签表达式必须是 (departmentId:123 &amp;&amp; template2)?
    • 是的,完全正确。在某些情况下,无法使用表达式。我使用了后缀示例,因为根据您的代码,我以某种方式假设您针对的是单个用户。但是,如果您的目标是部门,那么表达式正是执行此操作的机制。
    • 我会说 80% 的时间我针对的是个人用户 (userId:123456),但有时我针对的是部门 (departmentId:123)。创建具有不同名称的相同模板的副本会更好(一个具有您提到的后缀策略),以便80%的用例可以更快,或者为了实现简单,我应该只使用单个模板并始终使用标签表达式?
    猜你喜欢
    • 1970-01-01
    • 2017-02-24
    • 2021-04-18
    • 1970-01-01
    • 2012-08-02
    • 2020-04-26
    • 2020-12-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多