Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/313.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何向特定的通知中心安装模板发送通知?_C#_.net_Azure_Azure Notificationhub - Fatal编程技术网

C# 如何向特定的通知中心安装模板发送通知?

C# 如何向特定的通知中心安装模板发送通知?,c#,.net,azure,azure-notificationhub,C#,.net,Azure,Azure Notificationhub,我正在使用多个模板从.NET后端服务器代码注册安装。例如: var installation = new Installation { InstallationId = id, PushChannel = token, Templates = new Dictionary<string, InstallationTemplate>(), Tags = new List<string> { "userId:123456" }, Pla

我正在使用多个模板从.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);
var安装=新安装
{
InstallationId=id,
PushChannel=令牌,
Templates=新字典(),
Tags=新列表{“userId:123456”},
平台=通知平台.Gcm
};
installation.Templates.Add(“template1”,新安装模板{Body=“{\”data\”:{\“message\”:\“$(message)\“}}}”);
Add(“template2”,新安装模板{Body=“{\”data\”:{\“message2\”:\”$(message)\“}}});
wait_client.CreateOrUpdateInstallationAsync(安装);
发送通知时,如何针对特定模板?我在SDK中只看到以下内容:

await _client.SendTemplateNotificationAsync(
    new Dictionary<string, string>
    {
        { "message",  "Hello world." }
    }, "userId:123456");
wait_client.SendTemplateNotificationAsync(
新词典
{
{“消息”,“你好,世界。”}
},“用户ID:123456”);
SendTemplateNotificationAsync
方法没有任何参数可用于指定目标模板(例如,
template2

将使用哪个模板?我在这里误解了什么吗?

标记
属性。这是区分模板的一种方法


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

因此,通过
SendTemplateNotificationAsync
上的
tagExpression
将特定模板作为目标?在上面的示例中,我是否可以像
(userId:123456&&template2)
那样执行
tagExpression
?多个用户可以具有相同的模板ID吗?例如,如果我为另一个用户执行了相同的安装,
userId:789
,并使用
tagExpression
(userId:789&&template2)
向他们发送相同类型的通知。我想您可以这样做,但与发送到单个标记相比,表达式求值会使其速度变慢。因此,如果您可以以一种独特的方式标记单个模板(从而避免使用模板表达式),那么它将运行得更快。如果我只针对一个用户,那么创建您所描述的标记后缀会起作用,但是,当我想使用同一模板发送给多个用户时,我有一些用例。例如,多个用户有一个
部门ID:123
标记,我希望向他们发送相同的消息(模板)。我猜这种情况下的标记表达式必须类似于
(departmentId:123&&template2)
?是的,没错。在某些情况下,没有办法使用表达式。我使用后缀示例是因为基于您的代码,我不知何故假设您的目标用户是个人用户。但如果你的目标是部门,那么表达式正是实现这一目标的机制。我会说,80%的时间我是针对个人用户(
userId:123456
),但有时我是针对部门(
departmentId:123
)。创建具有不同名称的同一模板的副本(其中一个具有您提到的后缀策略)是否更好,这样80%的用例可以更快,或者为了实现的简单性,我是否应该只使用一个模板并始终使用标记表达式?