【问题标题】:Sending notification to Azure Notification Hub from Universal Windows Platform (UWP) app从通用 Windows 平台 (UWP) 应用向 Azure 通知中心发送通知
【发布时间】:2016-08-14 19:47:51
【问题描述】:

正如标题所示,我需要向我的 Azure 中心发送通知FROM一个 UWP 应用(用 C# 编写)(然后从那里发送到我已经创建的 Android 应用)。我显然使用 GCM 向我的 Android 应用发送推送通知。

经过无数小时的搜索,我还没有找到一个有用的教程,因为他们中的大多数使用控制台应用程序来发送通知,而不是通用 Windows 平台应用程序。

如果有人可以帮助我,我会真的感激不尽。

【问题讨论】:

  • 你能展示你使用的代码吗?通过 c# 发送推送通知总是一样的......无论您使用的是 UWP、WPF、Forms 还是控制台应用程序。
  • @HeribertScharnagl 很高兴。这是链接:azure.microsoft.com/en-us/documentation/articles/… ... 对于我的 android 应用程序,我使用了这个:azure.microsoft.com/en-us/documentation/articles/… ... 我需要做的基本上就是翻译下面的功能“(可选)直接从该应用程序”在指向 C# 的链接中。但问题是我从来没有用 C# 写过代码。

标签: c# azure notifications win-universal-app azure-notificationhub


【解决方案1】:

您可以使用通知中心 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

【讨论】:

    【解决方案2】:

    我将在这里回答我自己的问题,因为很多人都像我一样为此苦苦挣扎。下面的代码通过 Azure 通知中心将通知从通用 Windows 平台 (UWP) 发送到 Android 应用程序(使用 GCM)。

    请注意,必须稍微更改代码才能在您自己的通知中心上运行(有关详细信息,请参阅代码中的 cmets)

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

    【讨论】:

      猜你喜欢
      • 2015-11-19
      • 2014-03-03
      • 1970-01-01
      • 2017-06-02
      • 2017-02-24
      • 1970-01-01
      • 1970-01-01
      • 2021-04-18
      • 2016-02-24
      相关资源
      最近更新 更多