NotificationHub 在注册标签 UWP 中发送通知
Posted
技术标签:
【中文标题】NotificationHub 在注册标签 UWP 中发送通知【英文标题】:NotificationHub send notification in registered tag UWP 【发布时间】:2016-04-13 14:19:20 【问题描述】:有没有办法通过 NotificationHub 使用 UWP 应用发送通知?在许多教程中,他们使用 Microsoft.Azure.NotificationHubs Nuget 包通过 C# 控制台应用程序发送通知。我无法在 UWP 应用中安装此包。 我可以专门向我在 RegisterNativeAsync 注册的标记设备发送通知吗?
【问题讨论】:
【参考方案1】:根据我的经验,我认为在 UWP 应用中发送通知的简单方法是通过 HttpClient 使用通知中心 REST API,而不存在平台兼容性问题。
请参考文档Notification Hubs REST APIs。
您可以尝试参考文档Using REST APIs from a Backend
将您的UWP 应用程序作为后端来发送消息。例如,Send a WNS Native Notification。
希望对你有帮助。
【讨论】:
这很有帮助。我现在正在尝试使用HttpRequestMessage
和 HttpClient
发送通知,但我收到 500 Internal Server 错误。你知道为什么会这样吗?我正在使用this authentication
@Jerrak0s 你能分享你的代码来帮助分析这个问题吗?
using (var httpClient = new HttpClient()) var request = new HttpRequestMessage(HttpMethod.Post,uri); request.Content = new StringContent(toast); request.Headers.Add("Authorization",getSaSToken(URL,1000)); request.Headers.Add("ServiceBusNotification-Tags","TAG"); request.Headers.Add("ServiceBusNotification-Format","windows"); var response = await httpClient.SendAsync(request); await response.Content.ReadAsStringAsync();
var toast = @"<toast><visual><binding template=""ToastText01""><text id=""1"">Hello</text></binding></visual></toast>"; var uri = "https://NAMESPACE.servicebus.windows.net/NAME/messages/?api-version=2015-04";
@Jerrak0s Authorization
标头的值为“SharedAccessSignature <sas-token>
”,请参阅文档Common Concepts
的Create SAS Security Token
部分。【参考方案2】:
我终于找到了解决办法。代码就像@Kyle,但我必须添加
request.Headers.Add("X-WNS-Type", "wns/toast");
为了发送推送通知
More details
【讨论】:
【参考方案3】:这是一个通过 Azure 通知中心从 UWP 发送通知的工作代码(但是,它使用 GCM 而不是 WNS,请参阅如何更改已批准答案中的代码)。它显然需要一些更改,例如您的集线器名称,请参阅代码中的 cmets 了解更多信息。
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Runtime.InteropServices.WindowsRuntime;
using System.Text;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.Security.Cryptography;
using Windows.Security.Cryptography.Core;
using Windows.Storage.Streams;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
namespace SendNotification
public sealed partial class MainPage : Page
public MainPage()
this.InitializeComponent();
this.sendNotification();
string Endpoint = "";
string SasKeyName = "";
string SasKeyValue = "";
public void ConnectionStringUtility(string connectionString)
//Parse Connectionstring
char[] separator = ';' ;
string[] parts = connectionString.Split(separator);
for (int i = 0; i < parts.Length; i++)
if (parts[i].StartsWith("Endpoint"))
Endpoint = "https" + parts[i].Substring(11);
if (parts[i].StartsWith("SharedAccessKeyName"))
SasKeyName = parts[i].Substring(20);
if (parts[i].StartsWith("SharedAccessKey"))
SasKeyValue = parts[i].Substring(16);
public string getSaSToken(string uri, int minUntilExpire)
string targetUri = Uri.EscapeDataString(uri.ToLower()).ToLower();
// Add an expiration in seconds to it.
long expiresOnDate = DateTime.Now.Ticks / TimeSpan.TicksPerMillisecond;
expiresOnDate += minUntilExpire * 60 * 1000;
long expires_seconds = expiresOnDate / 1000;
String toSign = targetUri + "\n" + expires_seconds;
// Generate a HMAC-SHA256 hash or the uri and expiration using your secret key.
MacAlgorithmProvider macAlgorithmProvider = MacAlgorithmProvider.OpenAlgorithm(MacAlgorithmNames.HmacSha256);
BinaryStringEncoding encoding = BinaryStringEncoding.Utf8;
var messageBuffer = CryptographicBuffer.ConvertStringToBinary(toSign, encoding);
IBuffer keyBuffer = CryptographicBuffer.ConvertStringToBinary(SasKeyValue, encoding);
CryptographicKey hmacKey = macAlgorithmProvider.CreateKey(keyBuffer);
IBuffer signedMessage = CryptographicEngine.Sign(hmacKey, messageBuffer);
string signature = Uri.EscapeDataString(CryptographicBuffer.EncodeToBase64String(signedMessage));
return "SharedAccessSignature sr=" + targetUri + "&sig=" + signature + "&se=" + expires_seconds + "&skn=" + SasKeyName;
public async void sendNotification()
ConnectionStringUtility("YOURHubFullAccess"); //insert your HubFullAccess here (a string that can be copied from the Azure Portal by clicking Access Policies on the Settings blade for your notification hub)
//replace YOURHUBNAME with whatever you named your notification hub in azure
var uri = Endpoint + "YOURHUBNAME" + "/messages/?api-version=2015-01";
string json = "\"data\":\"message\":\"" + "Hello World!" + "\"";
//send an HTTP POST request
using (var httpClient = new HttpClient())
var request = new HttpRequestMessage(HttpMethod.Post, uri);
request.Content = new StringContent(json);
request.Headers.Add("Authorization", getSaSToken(uri, 1000));
request.Headers.Add("ServiceBusNotification-Format", "gcm");
var response = await httpClient.SendAsync(request);
await response.Content.ReadAsStringAsync();
【讨论】:
这对你有用吗?我收到 400 错误请求错误 @Jerrak0s 是的,我已经测试过多次,即使在不同的计算机上也是如此。仔细检查您的 HubFullAccess 字符串和集线器的名称,并确保您在代码中的正确位置输入它们。还要确保您已正确配置集线器,我个人使用本指南进行配置:azure.microsoft.com/en-us/documentation/articles/… 如果您有任何其他问题,请告诉我,我会尽力为您提供帮助。【参考方案4】:您是否尝试过 WindowsAzure.Messaging.Managed?刚刚用 W10 UWP 试了一下。
https://azure.microsoft.com/en-us/documentation/articles/notification-hubs-windows-store-dotnet-get-started/
【讨论】:
当然可以,但我找不到发送通知的示例。仅使用 NotificationHub 类接收。以上是关于NotificationHub 在注册标签 UWP 中发送通知的主要内容,如果未能解决你的问题,请参考以下文章
Azure NotificationHub - 检测失败的通知
为啥我在 Windows 10 UWP 上收到“无法注册包”部署错误?
Azure NotificationHub 发送推送时凭据无效