[英]NotificationHub send notification in registered tag UWP
Is there a way to send a notification with UWP app through NotificationHub? 有没有办法通过NotificationHub向UWP应用发送通知? In many tutorials they send notifications with a C# console application using Microsoft.Azure.NotificationHubs Nuget package.
在许多教程中,他们使用Microsoft.Azure.NotificationHubs Nuget包向C#控制台应用程序发送通知。 I cannot install this package in a UWP app.
我无法在UWP应用程序中安装此软件包。 Can i specifically send a notification to a tagged device that i register with RegisterNativeAsync?
我可以专门向我注册RegisterNativeAsync的标记设备发送通知吗?
Per my experience, I think the simple way for sending notification in a UWP app is to using Notification Hub REST APIs via HttpClient, without the issues for platform compatibility. 根据我的经验,我认为在UWP应用程序中发送通知的简单方法是通过HttpClient使用Notification Hub REST API,而不存在平台兼容性问题。
Please refer to the document Notification Hubs REST APIs . 请参阅文档Notification Hubs REST API 。
You can try to refer to the doc Using REST APIs from a Backend
to make your UWP app as a backend to send messages. 您可以尝试
Using REST APIs from a Backend
引用文档Using REST APIs from a Backend
将您的UWP应用程序作为后端发送消息。 For example, Send a WNS Native Notification . 例如, 发送WNS本机通知 。
Hope it helps. 希望能帮助到你。
Here's a working code that sends a notification from a UWP through an Azure Notification Hub (However, it uses GCM instead of WNS, see how to change the code in the approved answer). 这是一个工作代码,通过Azure通知中心从UWP发送通知(但是,它使用GCM而不是WNS,请参阅如何更改已批准答案中的代码)。 It obviously needs a few changes like your hub's name, see the comments in the code for more info.
它显然需要一些更改,如您的集线器名称,请参阅代码中的注释以获取更多信息。
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();
}
}
}
}
I finally found the solution. 我终于找到了解决方案。 The code is like @Kyle but i had to add
代码就像@Kyle,但我必须添加
request.Headers.Add("X-WNS-Type", "wns/toast");
in order to send a push notification 为了发送推送通知
Did you try WindowsAzure.Messaging.Managed? 你尝试过WindowsAzure.Messaging.Managed吗? Just tried it with the W10 UWP.
刚尝试使用W10 UWP。
https://azure.microsoft.com/en-us/documentation/articles/notification-hubs-windows-store-dotnet-get-started/ https://azure.microsoft.com/en-us/documentation/articles/notification-hubs-windows-store-dotnet-get-started/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.