从通用 Windows 平台 (UWP) 应用向 Azure 通知中心发送通知
Sending notification to Azure Notification Hub from Universal Windows Platform (UWP) app
如标题所示,我需要从 UWP 应用程序(用 C# 编写)向我的 Azure 中心发送通知 (并从那里发送到 Android 我已经创建的应用程序)。我显然使用 GCM 来向我的 Android 应用程序发送推送通知。
经过无数小时的搜索,我还没有找到一个可以以某种方式使用的教程,因为它们中的大多数使用控制台应用程序来发送通知,而不是通用 Windows 平台应用程序.
如果有人能帮助我,我会真的感激不尽。
您可以使用通知中心 REST APIs 通过 vanilla HTTP/HTTPS.
从任何地方(后端或设备)推送通知
这里有一个示例(使用 Java 客户端):https://msdn.microsoft.com/en-us/library/azure/dn495628.aspx
API 参考在这里:https://msdn.microsoft.com/en-us/library/azure/dn495827.aspx
我要在这里回答我自己的问题,因为很多人都像我一样为此苦苦挣扎。因此,这是一个代码,它通过 Azure 通知中心从通用 Windows 平台 (UWP) 向 Android 应用程序(使用 GCM)发送通知。
请注意,必须稍微更改代码 才能在您自己的 通知中心上工作(请参阅代码中的注释更多详细信息)
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()
{
//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)
ConnectionStringUtility("YOURHubFullAccess");
//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();
}
}
}
}
如标题所示,我需要从 UWP 应用程序(用 C# 编写)向我的 Azure 中心发送通知 (并从那里发送到 Android 我已经创建的应用程序)。我显然使用 GCM 来向我的 Android 应用程序发送推送通知。
经过无数小时的搜索,我还没有找到一个可以以某种方式使用的教程,因为它们中的大多数使用控制台应用程序来发送通知,而不是通用 Windows 平台应用程序.
如果有人能帮助我,我会真的感激不尽。
您可以使用通知中心 REST APIs 通过 vanilla HTTP/HTTPS.
从任何地方(后端或设备)推送通知这里有一个示例(使用 Java 客户端):https://msdn.microsoft.com/en-us/library/azure/dn495628.aspx
API 参考在这里:https://msdn.microsoft.com/en-us/library/azure/dn495827.aspx
我要在这里回答我自己的问题,因为很多人都像我一样为此苦苦挣扎。因此,这是一个代码,它通过 Azure 通知中心从通用 Windows 平台 (UWP) 向 Android 应用程序(使用 GCM)发送通知。
请注意,必须稍微更改代码 才能在您自己的 通知中心上工作(请参阅代码中的注释更多详细信息)
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()
{
//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)
ConnectionStringUtility("YOURHubFullAccess");
//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();
}
}
}
}