c# - 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:
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?
解决方案
好的,我想通了,这里有同样问题的人:
首先,其他人也可能对此感到困惑,我们需要了解定义推送模板的概念与 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 一起使用。这一定非常棒。
推荐阅读
- flutter - 在 PageView 中使用 SingleChildScrollView
- python - 在 OpenCV 中将 RGBA 渐变添加到 RGB 图像
- html - 如何最小化要在移动设备上设置的图像
- c# - 关于简单认证和授权系统的问题
- ios - Flutter:如何使用flutter_messaging插件在ios上显示丰富的通知?
- java - Java:无法在 timestamp.HBASE api 处执行列族删除
- monaco-editor - 将 OCaml 中定义的自定义语言添加到 Monaco Editor
- bluetooth - 蓝牙已连接但启动时无法使用
- google-apps-script - 从电子表格自定义列中检索值并放入模板文档
- c - 为什么编译器将赋值解释为定义