尝试使用Azure Notification Hub批量发送时禁止403

时间:2017-04-07 08:28:59

标签: azure push-notification azure-notificationhub

我正在尝试使用批量发送API端点根据用户的兴趣向特定用户发送大量推送通知。我们得出结论,通知中心中的标签系统无法提供我们所需的灵活性。

我有一个存储可以由直接发送端点使用的设备令牌的数据库,但即使使用Microsoft提供的标准信息也会提供403错误,但没有关于出错的信息。

public class WnsNotificationService : BaseNotificationService
{
    private ISubscriptionProvider subscriptionProvider;

    /// <summary>
    /// Initializes a new instance of the <see cref="WnsNotificationService" /> class.
    /// </summary>
    /// <param name="telemetryService">The telemetry service.</param>
    /// <param name="subscriptionProvider">The subscription provider.</param>
    public WnsNotificationService(ITelemetryService telemetryService, ISubscriptionProvider subscriptionProvider) : base(telemetryService)
    {
        this.subscriptionProvider = subscriptionProvider;
    }

    /// <summary>
    /// Sends the new Airport Event notification to windows devices via WNS.
    /// </summary>
    /// <param name="airportEvent">The Airport Event.</param>
    /// <returns>
    /// Notification Result
    /// </returns>
    public override async Task<NotificationResult> SendNotification(AirportEvent airportEvent)
    {
        try
        {
            IEnumerable<IEnumerable<string>> deviceCollection =
                this.subscriptionProvider.GetSubscribedUserPNSHandles(airportEvent, PushNotificationPlatform.wns).Batch(1000);

            foreach (var devices in deviceCollection)
            {
                ServiceBusConnectionStringBuilder connectionString = new ServiceBusConnectionStringBuilder(ConfigurationManager.AppSettings["Microsoft.Azure.NotificationHubs.ConnectionString"]);
                string serviceBusNamespace = connectionString.Endpoints.First().Host;
                string namespaceKeyName = connectionString.SharedAccessKeyName;
                string namespaceKey = connectionString.SharedAccessKey;

                var uri = new Uri($"{ ConfigurationManager.AppSettings["Microsoft.Azure.NotificationHubs.Url"] }/{ ConfigurationManager.AppSettings["Microsoft.Azure.NotificationHubs.HubName"] }/messages/$batch?direct&api-version=2015-08");
                var request = WebRequest.CreateHttp(uri);
                request.Method = "POST";
                request.ContentType = @"multipart/mixed; boundary = ""simple-boundary""";
                request.Headers["Authorization"] = SharedAccessSignatureTokenProvider.GetSharedAccessSignature(namespaceKeyName, namespaceKey, serviceBusNamespace, TimeSpan.FromMinutes(45));
                request.Headers["ServiceBusNotification-Format"] = "windows";
                request.Headers["X-WNS-Type"] = "wns/raw";

                string body = this.GenerateBatchRequestBody(airportEvent.AirportEventId, devices);

                byte[] requestBytes = new ASCIIEncoding().GetBytes(body);
                Stream requestStream = request.GetRequestStream();
                requestStream.Write(requestBytes, 0, requestBytes.Length);

                request.GetResponse();
            }

            return new NotificationResult(true);
        }
        catch (Exception)
        {
            return new NotificationResult(false);
        }
    }

    /// <summary>
    /// Sends the new Airport Event notification to a specific windows device via WNS.
    /// </summary>
    /// <param name="deviceId">The device identifier.</param>
    /// <param name="airportEvent">The Airport Event.</param>
    /// <returns>
    /// Notification Result
    /// </returns>
    /// <exception cref="System.NotImplementedException">Not Implemented Exception</exception>
    public override Task<NotificationResult> SendNotificationToDevice(string deviceId, AirportEvent airportEvent)
    {
        throw new NotImplementedException();
    }

    /// <summary>
    /// Generates the batch request body.
    /// </summary>
    /// <param name="airportEventGuid">The airport event unique identifier.</param>
    /// <param name="devices">The devices.</param>
    /// <returns>Request Body</returns>
    private string GenerateBatchRequestBody(Guid airportEventGuid, IEnumerable<string> devices)
    {
        return @"
--simple-boundary
Content-type: text/xml
Content-Disposition: inline; name=notification

<toast><visual><binding template=""ToastText01""><text id=""1"">Hello from 
Batch Direct Send!</text></binding></visual></toast>
--simple-boundary
Content-type: application/json
Content-Disposition: inline; name=devices

['https://{foo}.notify.windows.com/?token={bar}']
--simple-boundary--";
    }
}

这是使用https://azure.microsoft.com/en-us/blog/push-notification-hubs-batch-direct-send/

提供的信息

1 个答案:

答案 0 :(得分:2)

根据您的说明,我检查了Direct Batch Send并按照此示例azure-notificationhubs-samples来测试此问题。经过一些试验,我可以按照以下方式在我身边工作:

enter image description here

根据您的代码,我认为您需要将request.Headers["X-WNS-Type"] = "wns/raw";更改为request.Headers["X-WNS-Type"] = "wns/toast";,并且您的授权令牌可能无效。这是我生成授权令牌的方法,您可以参考它:

/// <summary>
/// GetSharedAccessSignature
/// </summary>
/// <param name="SasKeyName">SharedAccessKeyName</param>
/// <param name="SasKeyValue">SharedAccessKey</param>
/// <param name="uri">resourceURI (e.g. https://{namespace}.servicebus.windows.net/{NotificationHub}/messages/$batch?direct&api-version=2015-08)</param>
/// <param name="minUntilExpire">minUntilExpire</param>
/// <returns></returns>
private static string GetSharedAccessSignature(string SasKeyName, string SasKeyValue, string uri, TimeSpan minUntilExpire)
{
    string targetUri = Uri.EscapeDataString(uri.ToLower()).ToLower();

    // Add an expiration in seconds to it.
    long expiresOnDate = DateTime.Now.Ticks / TimeSpan.TicksPerMillisecond;
    expiresOnDate += (long)minUntilExpire.TotalMilliseconds;
    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.
    byte[] keyBytes = System.Text.Encoding.UTF8.GetBytes(SasKeyValue);
    HMACSHA256 hmacsha256 = new HMACSHA256(keyBytes);
    byte[] hash = hmacsha256.ComputeHash(System.Text.Encoding.UTF8.GetBytes(toSign));

    // Create the token string using the base64
    string signature = Uri.EscapeDataString(Convert.ToBase64String(hash));

    return "SharedAccessSignature sr=" + targetUri + "&sig=" + signature + "&se=" + expires_seconds + "&skn=" + SasKeyName;
}

注意:以下是官方代码示例,您可以参考it

此外,正如Direct Batch Send所述:

  

将一批通知直接发送到设备句柄集合(由通知类型表示的有效令牌)。此API适用于Basic and Standard tier Notification Hub namespaces

您需要将定价等级更改为基本或标准,否则您将通过Fiddler获得以下回复:

enter image description here