首页 > 解决方案 > How to use azure notification hub tags for push messages in UWP apps

问题描述

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://docs.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://docs.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: enter image description here

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":

enter image description here

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?

标签: c#azureuwpazure-notificationhub

解决方案


好的,我想通了,这里有同样问题的人:

首先,其他人也可能对此感到困惑,我们需要了解定义推送模板的概念与 FCM(适用于 Android)的工作方式不同。在 FCM 中,您定义推送消息服务器端的布局和内容。

在 UWP 中,它在使用标签时发生在客户端。在设计 toast 时,您可以将变量放入其中,然后由服务器端填充。

这是工作代码。

客户端:

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

服务器端:

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

您可以从网络上使用“自定义模板”平台发送消息: 在此处输入图像描述

不确定“自定义模板”是否也可以与 android 和 iOS 一起使用。这一定非常棒。


推荐阅读