如何在使用Microsoft.Azure.NotificationHubs.Installation类时选择模板
本文关键字:Installation 选择 NotificationHubs Azure Microsoft | 更新日期: 2023-09-27 17:53:06
我正在建立一个使用Azure推送通知的Web API项目。我想使用新的"安装"模式而不是旧的"注册"模式。然而,文档有一些限制。
查看MSDN, Microsoft.Azure.NotificationHubs.Installation
具有Tags
属性。
也有一个Templates
属性。模板类型是InstallationTemplate
,令人惊讶的是还有Tags
。
模板不仅仅是一个列表,而是一个从string
映射到InstallationTemplate
的字典。
我理解标签背后的思想。但是我对两个标签属性和字典的键感到困惑。
我看到了一个例子,其中Installation.Tag
被设置为"foo",然后两个模板添加了关键字"template1"answers"template2"。InstallationTemplate.Tag
设置为"tag-for-template1"answers"tag-for-template2"
- 两个标签值必须匹配吗?
- 它们各自的用途是什么?
- 模板字典的键应该是什么?
- 如果我使用该方法通过NotificationHubClient发送通知,我可以指定一个标签-它与哪个模板匹配,将被选中?
经过一些测试,我有点聪明了。
当使用SendTemplateNotificationAsync()
时,Microsoft.Azure.NotificationHubs.Installation
的标签和 InstallationTemplate
内部的标签都将被评估。必须在字典中指定的键似乎没有使用。
例子:
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"answers"subtemplate2"的任何组合。
键"someKey"answers"someOtherKey"将不会被查询使用,我不明白为什么他们不使用List<T>
而不是字典。