我嘗試使用批處理時禁止發送API端點一些推送通知發送到基於自身利益的特定用戶。我們得出的結論是,通知中心的標籤系統不能提供我們所需的靈活性。403試圖使用Azure的通知中心批量發送
我有一個存儲可以通過直接發送端點可以使用,但即使使用微軟提供的提供了一個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/
好極了,爲全面回答布魯斯的感謝! – JamieB