Reputation: 597
I want to send push messages to apps in specific channels, like "en-us" and "fr-fr" to localize the push notifications.
First i followed this tutorial, and it all worked: https://learn.microsoft.com/en-us/azure/notification-hubs/notification-hubs-windows-store-dotnet-get-started-wns-push-notification
the working registration there is:
var result = await hub.RegisterNativeAsync(channel.Uri);
But thats to send one message to all clients. Then i followed this tutorial: https://learn.microsoft.com/en-us/azure/notification-hubs/notification-hubs-windows-notification-dotnet-push-xplat-segmented-wns
and from what i could extract from that confusing mix with uwp code is this line:
var hub = new NotificationHub("AppName", "endpoint");
const string templateBodyWNS = "<toast><visual><binding template=\"ToastText01\"><text id=\"1\">$(messageParam)</text></binding></visual></toast>";
var result = await hub.RegisterTemplateAsync(channel.Uri, templateBodyWNS, "simpleWNSTemplateExample", new string[] { "en-us" });
Result also gives me a valid registration.
Then i tried to test it using the azure notfication hub console (that worked with the previous step to send it to all clients:
which resulted in the app getting the notification (it does not filter for "en-us"). Then i tried to put "en-us" in the "send to tag expression":
With that, no toast message arrives.
Then i tried to send the message via Microsoft.Azure.NotificationHubs package.
This code works:
NotificationHubClient Hub = NotificationHubClient.CreateClientFromConnectionString(endpoint, name);
string toast = @"<?xml version='1.0' encoding='utf-8'?>
<toast>
<visual><binding template='ToastText01'>
<text id='1'> Test message </text>
</binding>
</visual>
</toast>
";
var result = await Hub.SendWindowsNativeNotificationAsync(toast);
A Toast Message arrives. But as soon as i change the last line to:
var result = await Hub.SendWindowsNativeNotificationAsync(toast, "en-us");
Nothing arrives. So Notification Hub is successfully linked to the client via WNS, but using tags does not work at all. What do i do wrong?
Upvotes: 2
Views: 1289
Reputation: 597
Okay i figured it out, here for anyone with the same question:
First, as someone else might get confused with this as well, is that we need to understand that the concept of defining the Push Templates is different from how FCM (for Android) works. In FCM, you define the layout and content of the push message server-side.
In UWP, it happens client side when using tags. When designing a toast you can put variables inside it, which then get filled by the serverside.
This is the working code.
Client side:
var hub = new NotificationHub("Hubname", "endpoint");
string toast = @"<toast>
<visual><binding template='ToastGeneric'>
<text id='1'>$(Title)</text>
<text id='2'>$(Message)</text>
<text placement='attribution'>via SMS</text>
</binding>
</visual>
</toast>
";
var result = await hub.RegisterTemplateAsync(channel.Uri, toast, localizedWNSTemplateExample", new string[] { "myTag" });
Serverside:
NotificationHubClient Hub = NotificationHubClient.CreateClientFromConnectionString(endpoint, name);
Dictionary<string, string> templateParams = new Dictionary<string, string>();
templateParams["Title"] = "Title here";
templateParams["Message"] = "Message here";
await Hub.SendTemplateNotificationAsync(templateParams, "myTag");
And from the web you can send messages with the "Custom Template" Platform:
Not sure if "Custom Template" can also be used with android and iOS as well though. It would be awesome.
Upvotes: 1