加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – UWP推送通知

发布时间:2020-12-15 20:59:58 所属栏目:百科 来源:网络整理
导读:这适用于 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 t
这适用于 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();

通过上面的这一行,我得到了一个大约一个月有用的网址.

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

关于如何使用Uri以及如何将信息发送给它的任何指示我都会很高兴,因为我有400错误,这与我的帖子消息有关.

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

也:

我不拥有或使用任何基于云的Web服务.

我可以在发布或调试模式下获得推送通知吗?

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

解决方法

刚刚完成Jerrak0s的解决方案

鉴于您的Azure通知中心:

enter image description here

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

private static async void SendNotificationAsync()
{
        //SettingAccess policiesDefaultFullSharedAccessSignature 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令牌的Utility类:

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读