in src/Microsoft.Azure.NotificationHubs/NotificationHubClient.cs [2915:2975]
public async Task<NotificationOutcome> SendDirectNotificationAsync(Notification notification, IList<string> deviceHandles, CancellationToken cancellationToken)
{
if (notification == null)
{
throw new ArgumentNullException(nameof(notification));
}
if (deviceHandles==null)
{
throw new ArgumentNullException(nameof(deviceHandles));
}
if (deviceHandles.Count == 0)
{
throw new ArgumentException(message: $"{nameof(deviceHandles)} should contain at least one value", paramName: nameof(deviceHandles));
}
var requestUri = GetGenericRequestUriBuilder();
requestUri.Path += "messages/$batch";
AddToQuery(requestUri, "&direct");
// Convert FcmNotification into GcmNotification
notification = FcmToGcmNotificationTypeCast(notification);
notification.ValidateAndPopulateHeaders();
return await _retryPolicy.RunOperation(async (ct) =>
{
using (var request = CreateHttpRequest(HttpMethod.Post, requestUri.Uri, out var trackingId))
{
foreach (var item in notification.Headers)
{
request.Headers.Add(item.Key, item.Value);
}
var content = new MultipartContent("mixed", "nh-batch-multipart-boundary");
ParseContentType(notification.ContentType, out var mediaType, out var encoding);
var notificationContent = new StringContent(notification.Body, encoding, mediaType);
notificationContent.Headers.ContentDisposition = new ContentDispositionHeaderValue("inline") { Name = "notification" };
content.Add(notificationContent);
var devicesContent = new StringContent(JsonConvert.SerializeObject(deviceHandles), Encoding.UTF8, "application/json");
devicesContent.Headers.ContentDisposition = new ContentDispositionHeaderValue("inline") { Name = "devices" };
content.Add(devicesContent);
request.Content = content;
using (var response = await SendRequestAsync(request, trackingId, HttpStatusCode.Created, ct).ConfigureAwait(false))
{
return new NotificationOutcome()
{
State = NotificationOutcomeState.Enqueued,
TrackingId = trackingId,
NotificationId = GetNotificationIdFromResponse(response)
};
}
}
}, cancellationToken);
}