gpt4 book ai didi

c# - UWP 推送通知

转载 作者:可可西里 更新时间:2023-10-31 23:45:55 29 4
gpt4 key购买 nike

这是针对 Windows 10 的应用程序。我按照这个教程进行操作: https://msdn.microsoft.com/windows/uwp/controls-and-patterns/tiles-and-notifications-windows-push-notification-services--wns--overview

此时我得到了正确的下一个值:

SID

Secret

Uri(I got this on my application)

我得到的 Uri 是用这个简单的代码:

channel=await PushNotificationChannelManager.CreatePushNotificationChannelForApplicationAsync();

通过上面的行,我得到了一个可以使用大约一个月的 URL。

我现在无法做的部分是将推送通知发送到应用程序。

任何有关如何使用 Uri 以及如何向其发送信息的信息,我都会很高兴,因为我收到了 400 错误,这与我的帖子消息有问题有关。

此链接显示了如何发送信息,但我没有得到任何有用的代码。 https://msdn.microsoft.com/library/windows/apps/hh465435

另外:

我不拥有或使用任何基于云的网络服务。

我可以在发布或 Debug模式下收到推送通知吗?

可以使用 PHP 完成具有正确 Uri 的推送通知吗?

最佳答案

刚刚完成 Jerrak0s 的解决方案

鉴于您的 Azure 通知中心: enter image description here

使用 REST 发送推送通知的代码:

private static async void SendNotificationAsync()
{
//Setting\Access policies\DefaultFullSharedAccessSignature policy
string connStr = "Endpoint=sb://[namespace].servicebus.windows.net/;SharedAccessKeyName=DefaultFullSharedAccessSignature;SharedAccessKey=xyz";
var util = new ConnectionStringUtility(connStr);

//toast in XML
var toast = "<toast> <visual>
<binding template="ToastText01">
< text id = "1" > Test message </ text >
</ binding > </ visual > </ toast >";

//call WNS
var uri = "https://[namespace].servicebus.windows.net/[hub name]/messages/?api-version=2015-04";

using (var httpClient = new HttpClient())
{
var request = new HttpRequestMessage(HttpMethod.Post, uri);
request.Content = new StringContent(toast);
request.Headers.Add("Authorization", util.getSaSToken(uri, 1000));
//request.Headers.Add("ServiceBusNotification-Tags", "TAG");
request.Headers.Add("ServiceBusNotification-Format", "windows");
request.Headers.Add("X-WNS-Type", "wns/toast");
var response = await httpClient.SendAsync(request);
await response.Content.ReadAsStringAsync();
}
}

用于解析端点和 SAS token 的实用程序类:

public class ConnectionStringUtility
{
public string Endpoint { get; private set; }
public string SasKeyName { get; private set; }
public string SasKeyValue { get; private set; }

public 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;
}
}

关于c# - UWP 推送通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37336243/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com