Я пытаюсь использовать конечную точку API пакетной отправки для отправки ряда push-уведомлений конкретным пользователям в зависимости от их интересов. Мы пришли к выводу, что система тегов в центре уведомлений не обеспечивает необходимой нам гибкости.
У меня есть база данных, в которой хранятся токены устройств, которые могут использоваться конечной точкой прямой отправки, но даже использование стандартной информации, предоставленной 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/