如何将原始通知发送到azure通知中心

时间:2015-07-10 17:19:07

标签: azure notifications windows-phone-8.1 backend wns

我是azure的新手,我已经配置了我的应用程序以接收原始通知,并且我能够从azure网站的调试页面接收它们,我的问题是,我怎么能从我的后端发送它们?我能够发送所有类型的通知,但无法弄清楚如何发送该类型...一个非常简单的通知,如天蓝色的(Windows一个不是Windows手机),只有一个没有格式化的字符串

3 个答案:

答案 0 :(得分:5)

假设您正在使用WNS(而不是MPNS),只需从hub.wns对象调用SendRaw即可。语法是:

sendRaw(tags, payload, optionsOrCallbackopt, callback)

http://dl.windowsazure.com/nodedocs/WnsService.html的推送通知中心查看NodeJS文档中的WNS服务。

对于.NET后端,您使用https://msdn.microsoft.com/en-us/library/azure/dn369343.aspx中记录的 NotificationHubClient.SendNotificationAsync 。您输入的通知类将是{@ 3}}中描述的WindowsNotification。

由于您要发送原始通知,因此您必须自己创建有效内容。有关如何创建原始有效负载的文档位于https://msdn.microsoft.com/en-us/library/azure/microsoft.servicebus.notifications.windowsnotification.aspx,更具体地说是:

  • HTTP Content-Type标头必须设置为“application / octet-stream”。
  • HTTP X-WNS-Type标头必须设置为“wns / raw”。
  • 通知正文可以包含任何小于5 KB的字符串有效内容。

答案 1 :(得分:0)

以下是一些可以执行此任务的工作代码(符合.Net Standard 2.0):

  public static async Task SendRawNotification(string notificationString)
  {
    var sasToken = CreateSASToken($"http://{NotificationNamespace}.servicebus.windows.net/{NotificationHub}", "DefaultFullSharedAccessSignature", $"{SharedAccessKey}");

    var description = new NotificationDescription
    {
      NotificationType = type,
      Notification = JsonConvert.SerializeObject(notification)
    };
    var serialized = JsonConvert.SerializeObject(description);
    byte[] contentInBytes = Encoding.UTF8.GetBytes(serialized);

    HttpWebRequest request = WebRequest.Create($"https://{NotificationNamespace}.servicebus.windows.net/{NotificationHub}/messages/?api-version=2015-01") as HttpWebRequest;
    request.Method = "POST";
    request.ContentType = "application/octet-stream";
    request.Headers.Add("Authorization", sasToken);
    request.Headers.Add("X-WNS-Type", "wns/raw");
    request.Headers.Add("ServiceBusNotification-Format", "windows");


    using (Stream requestStream = request.GetRequestStream())
      requestStream.Write(contentInBytes, 0, contentInBytes.Length);

    using (HttpWebResponse webResponse = await request.GetResponseAsync() as HttpWebResponse)
    {
      if (webResponse.StatusCode != HttpStatusCode.Created)
      {
        throw new InvalidOperationException($"Failed to create notification: {serialized}");
      }
    }
  }

private static string CreateSASToken(string resourceUri, string keyName, string key)
{
  TimeSpan sinceEpoch = DateTime.UtcNow - new DateTime(1970, 1, 1);
  var expiry = Convert.ToString((int)sinceEpoch.TotalSeconds + 3600); //EXPIRES in 1h 
  string stringToSign = HttpUtility.UrlEncode(resourceUri) + "\n" + expiry;
  HMACSHA256 hmac = new HMACSHA256(Encoding.UTF8.GetBytes(key));

  var signature = Convert.ToBase64String(hmac.ComputeHash(Encoding.UTF8.GetBytes(stringToSign)));
  var sasToken = String.Format(CultureInfo.InvariantCulture,
  "SharedAccessSignature sr={0}&sig={1}&se={2}&skn={3}",
      HttpUtility.UrlEncode(resourceUri), HttpUtility.UrlEncode(signature), expiry, keyName);

  return sasToken;
}

此问题的先前答案具有无效链接,并且未提供有关如何实现此功能的有用信息。

不幸的是,截至编写此响应时,Azure Notification Hub SDK在使用NuGet软件包:Microsoft.Azure.NotificationHubs(2.0.0)时发送原始通知方面已被破坏

此功能可用,并且仍可在Microsoft.Azure.NotificationHubs(1.0.9)中使用,但是,此功能仅在以4.6.1为目标的.Net Framework项目中可用。

如果您希望在.Net Core或.Net Standard 2.0项目中发送原始通知,则将在发送时收到一个异常消息,类似于“您必须将内容类型设置为“应用程序/八位字节流””。这是因为,在SDK本身中,他们正在使用StringContent对象构造HttpRequest,该对象将“ charset-utf8”附加到ContentType标头中。

在发布对NuGet软件包的更新之前(Microsoft已经意识到此问题,并且当前正在努力提供更新),然后您必须自己创建HttpRequest。

不幸的是,再次严重缺乏有关如何实现此目的的Microsoft文档。

有几个步骤可以完成此操作,如下所示:

  1. 创建您的资源URI字符串

您尝试使用的资源的Uri。为了将请求发送到Azure通知中心,此文件的格式应如下所示: https://{NotificationHubNamespace}.servicebus.windows.net/{NotificationHubName}

因此,如果您的通知名称空间为“ notification-ns”,而通知中心称为“ notification-hub”,则URI应为: https://notification-ns.servicebus.windows.net/notification-hub

  1. 从Azure门户保存您的DefaultSharedAccessSignature

您需要从Azure门户复制下来DefaultFullSharedAccessSignature。您的签名应如下所示: Endpoint=sb://xxxx.servicebus.windows.net/;SharedAccessKeyName=DefaultFullSharedAccessSignature;SharedAccessKey=xxxxxxxxx=

复制/保存“ SharedAccessKey =“之后的部分,因为它用于生成访问令牌。

  1. 生成您的SharedAccessSignature令牌(SAS密钥)

调用CreateSASToken函数,将参数设置为以下内容: resourceUri:"https://{NotificationHubNamespace}.servicebus.windows.net/{NotificationHubName}" keyName: "DefaultFullSharedAccessSignature" key: the saved part following "SharedAccessKey=" in the DefaultFullSharedAccessSignature

这将生成格式正确的SharedAccessSignature (SAS Key)

现在,您可以使用System.Net.Http library创建HttpWebRequest来发送原始通知。

答案 2 :(得分:0)

此后,Microsoft工程师为我提供了以下示例程序,该程序也能够发送原始通知,我认为在此处共享它是适当的:

using Newtonsoft.Json.Linq;
using System;
using System.Globalization;
using System.Net.Http;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using System.Web;

namespace ConsoleApp
{
    class Program
    {
        static string NH_NAMESPACE = "{Please input}";
        static string HUB_NAME = "{Please input}";
        static string KEY_NAME = "DefaultFullSharedAccessSignature";
        static string KEY_VALUE = "{Please input}";

        static void Main(string[] args)
        {
            JObject jobject = new JObject();
            jobject.Add("text1", "my app");
            jobject.Add("text2", "my value");

            SendNotificaitonAsync(jobject.ToString()).Wait();

            Console.ReadLine();
        }

        private static async Task SendNotificaitonAsync(string content)
        {
            string resourceUri = $"https://{NH_NAMESPACE}.servicebus.windows.net/{HUB_NAME}/messages/";
            using (var request = CreateHttpRequest(HttpMethod.Post, resourceUri))
            {
                request.Content = new StringContent(content, Encoding.UTF8, "application/octet-stream");
                request.Content.Headers.ContentType.CharSet = string.Empty;
                var httpClient = new HttpClient();
                var response = await httpClient.SendAsync(request);
                Console.WriteLine(response.StatusCode);
            }
        }

        private static HttpRequestMessage CreateHttpRequest(HttpMethod method, String resourceUri)
        {
            var request = new HttpRequestMessage(method, $"{resourceUri}?api-version=2017-04");
            request.Headers.Add("Authorization", createToken(resourceUri, KEY_NAME, KEY_VALUE));
            request.Headers.Add("X-WNS-Type", "wns/raw");
            request.Headers.Add("ServiceBusNotification-Format", "windows");

            return request;
        }

        private static string createToken(string resourceUri, string keyName, string key)
        {
            TimeSpan sinceEpoch = DateTime.UtcNow - new DateTime(1970, 1, 1);
            var week = 60 * 60 * 24 * 7;
            var expiry = Convert.ToString((int)sinceEpoch.TotalSeconds + week);
            string stringToSign = HttpUtility.UrlEncode(resourceUri) + "\n" + expiry;
            HMACSHA256 hmac = new HMACSHA256(Encoding.UTF8.GetBytes(key));
            var signature = Convert.ToBase64String(hmac.ComputeHash(Encoding.UTF8.GetBytes(stringToSign)));
            var sasToken = String.Format(CultureInfo.InvariantCulture, "SharedAccessSignature sr={0}&sig={1}&se={2}&skn={3}", HttpUtility.UrlEncode(resourceUri), HttpUtility.UrlEncode(signature), expiry, keyName);
            return sasToken;
        }
    }
}