正如标题所示,我需要发送通知从 UWP应用程序(用C#编写)到我的Azure集线器(并从那里发送到我已经创建了一个Android应用程序)。我明显使用GCM来发送推送通知给我的Android应用程序。从通用Windows平台(UWP)应用程序发送通知到Azure通知集线器
搜索我还没有找到一个单一的教程,会在一定程度是有用的,因为他们大多数用在为了一个控制台应用程序发送通知,而不是一个通用的Windows平台的应用程序的无数个小时后。
如果任何人都可以请帮我,我会真的感谢。
正如标题所示,我需要发送通知从 UWP应用程序(用C#编写)到我的Azure集线器(并从那里发送到我已经创建了一个Android应用程序)。我明显使用GCM来发送推送通知给我的Android应用程序。从通用Windows平台(UWP)应用程序发送通知到Azure通知集线器
搜索我还没有找到一个单一的教程,会在一定程度是有用的,因为他们大多数用在为了一个控制台应用程序发送通知,而不是一个通用的Windows平台的应用程序的无数个小时后。
如果任何人都可以请帮我,我会真的感谢。
我会在这里回答我的问题,因为很多人有这种奋斗像我这样的。因此,这是一个通过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();
}
}
}
}
您可以使用通知中心的REST API可以从任何地方在香草HTTP/HTTPS(后端或设备)推送通知。
有一个样品(使用Java客户端)位置:https://msdn.microsoft.com/en-us/library/azure/dn495628.aspx
和API参考这里:https://msdn.microsoft.com/en-us/library/azure/dn495827.aspx
你能告诉你使用的代码工作?无论您使用UWP,WPF,Forms还是控制台应用程序,通过c#发送推送通知都是一样的。 –
@HeribertScharnagl很高兴。这里的链接:https://azure.microsoft.com/en-us/documentation/articles/notification-hubs-windows-store-dotnet-get-started/ ...和我的Android应用程序,我用这个:https://azure.microsoft.com/en-us/documentation/articles/notification-hubs-android-get-started/ ...我需要做的只是基本翻译下面的功能“(可选)发送直接从应用程序推送通知“的链接到C#。但问题是我从来没有用C#写过代码。 – Kyle