【问题标题】:How to select templates when using the Microsoft.Azure.NotificationHubs.Installation class?使用 Microsoft.Azure.NotificationHubs.Installation 类时如何选择模板?
【发布时间】:2016-11-01 15:41:01
【问题描述】:

我正在设置一个使用 Azure 推送通知的 Web API 项目。我想使用新的“安装”模型而不是旧的“注册”模型。但是文档有点有限。

查看MSDNMicrosoft.Azure.NotificationHubs.Installation 有一个Tags 属性。

还有一个Templatesproperty。模板类型为InstallationTemplatesurprisingly 也有Tags

模板不仅仅是一个列表,而是一个从string 映射到InstallationTemplate 的字典。

我理解标签背后的想法。但是我对这两个标签属性和字典的键感到困惑。

我看到了一个示例,其中Installation.Tag 设置为“foo”,然后添加了两个带有键“template1”和“template2”的模板。 InstallationTemplate.Tag 设置为“tag-for-template1”和“tag-for-template2”。

  • 这两个标签值是否必须匹配?
  • 它们各自的用途是什么?
  • 模板字典的key应该是什么?
  • 如果我使用该方法通过 NotificationHubClient 发送通知,我可以指定一个标签 - 它与什么匹配以及将选择哪个模板?

【问题讨论】:

    标签: c# azure push-notification azure-notificationhub


    【解决方案1】:

    经过一些测试,我有点聪明了。

    Microsoft.Azure.NotificationHubs.Installation 的标签InstallationTemplate 内的标签都将在使用SendTemplateNotificationAsync() 时进行评估。必须在字典中指定的键似乎未使用。

    示例:

    var installationA = new Installation();
    installationA.Tags = new List<string> { $"install-a" };
    installationA.Templates.Add("someKey", new InstallationTemplate
    {
        Body = "JSON-FOR-TEMPLATE"
        Tags = new List<string> { $"subtemplate1" }
    });
    installationA.Templates.Add("someOtherKey", new InstallationTemplate
    {
        Body = "JSON-FOR-TEMPLATE"
        Tags = new List<string> { $"subtemplate2" }
    });
    
    
    var installationB = new Installation();
    installationB.Tags = new List<string> { $"install-b" };
    installationB.Templates.Add("someKey", new InstallationTemplate
    {
        Body = "JSON-FOR-TEMPLATE"
        Tags = new List<string> { $"subtemplate1" }
    });
    installationB.Templates.Add("someOtherKey", new InstallationTemplate
    {
        Body = "JSON-FOR-TEMPLATE"
        Tags = new List<string> { $"subtemplate2" }
    });
    

    现在,您可以使用标记表达式过滤“install-a”、“install-b”、“subtemplate1”和“subtemplate2”的任意组合。

    查询不会使用键“someKey”和“someOtherKey”,我不明白他们为什么不使用List&lt;T&gt; 而不是字典。

    【讨论】:

    • 更新:InstallationTemplate 类型中的 Tags 属性已被标记为已弃用。
    猜你喜欢
    • 2016-02-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多