3

I am registering installations from my .NET backend server code with multiple templates each. For example:

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);

How do I target a specific template when sending a notification? All I see in the SDK is the following:

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

The SendTemplateNotificationAsync method does not have any parameters that let me specify which template I am targeting (for example, template2).

Which template will be used? Am I misunderstanding something here?

kspearrin
  • 10,238
  • 9
  • 53
  • 82

1 Answers1

2

InstallationTemplate class has Tags property. That's a way to differentiate between templates.

In your case, it looks like you could just skip tagging the entire installation via Installation.Tags property and use something like userId:123456-template tag on specific templates through InstallationTemplate.Tags. Then invoke SendTemplateNotificationAsync the same way you do, but with the template suffix.

Nikita R.
  • 7,245
  • 3
  • 51
  • 62
  • So a specific template is targeted via the `tagExpression` on `SendTemplateNotificationAsync`? With the above example, could I do a `tagExpression` like `(userId:123456 && template2)`? Can multiple users have the same template Ids? For example, if I did the same installation for another user, `userId:789` and send them the same type of notification with `tagExpression` `(userId:789 && template2)`. – kspearrin May 26 '17 at 17:15
  • I think you could, but expression evaluation makes it slower compared with sending to a single tag. So if you could tag individual templates in a unique way (and thus avoid template expressions), then it would work faster. – Nikita R. May 26 '17 at 17:19
  • Creating the tagging suffix like you have described works if I am only targeting a single user, however, I have use cases when I want to send to multiple users with the same template. For example, multiple users have a `departmentId:123` tag and I want to send them all the same message (template). I guess the tag expression for that situation would have to be something like `(departmentId:123 && template2)`? – kspearrin May 26 '17 at 17:24
  • Yes, exactly. In some cases, there's no way around using expressions. I used the suffix example because based on your code I somehow assumed that you're targeting individual users. But if you're targeting departments, then expressions is exactly the mechanism to go about it. – Nikita R. May 26 '17 at 17:28
  • I would say that 80% of the time I am targeting individual users (`userId:123456`), but sometimes I am targeting departments (`departmentId:123`). Would it be better to create duplicates of the same template with different names (one with the suffix strategy that you mentioned) so that the 80% use case can be faster or for implementation simplicity should I just use a single template and always use a tag expression? – kspearrin May 26 '17 at 17:30
  • `Tags` is a collection, so you could do both: keep one tag with a suffix and another tag just the template name. So you have a single template but could target it both ways. This will also simplify installation and template management compared to creating duplicates. – Nikita R. May 26 '17 at 17:37
  • Ok, wonderful. One last question here then. If `InstallationTemplate` uses the `Tags` property for it's identifiers, then why on the `Installation` is the `Templates` property a `Dictionary()`? What's the point of the `string` key? – kspearrin May 26 '17 at 17:40
  • Also, apparently the `Tags` property on `InstallationTemplate` is deprecated? – kspearrin May 26 '17 at 17:45
  • I think the `string` is the template identifier so that you can update it later. So there's a deprecated attribute on the property, but it still works? – Nikita R. May 26 '17 at 18:02
  • Ok, that makes sense then. Generally deprecated means that you shouldn't start using the property because there are plans for removal in the future. – kspearrin May 26 '17 at 18:04