private async Task PushNotifyInboxMessageWinStoreAsync(InboxEntity inbox, int failedAttempts = 0)
{
if (string.IsNullOrEmpty(inbox.ClientPackageSecurityIdentifier) || string.IsNullOrEmpty(inbox.PushChannelUri))
{
return;
}
var client = await this.ClientTable.GetAsync(inbox.ClientPackageSecurityIdentifier);
string bearerToken = client.AccessToken;
var pushNotifyRequest = new HttpRequestMessage(HttpMethod.Post, inbox.PushChannelUri);
pushNotifyRequest.Headers.Authorization = new AuthenticationHeaderValue("Bearer", bearerToken);
pushNotifyRequest.Headers.Add("X-WNS-Type", "wns/raw");
pushNotifyRequest.Content = new StringContent(inbox.PushChannelContent ?? string.Empty);
// yes, it's a string, but we must claim it's an octet-stream
pushNotifyRequest.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
var response = await this.HttpClient.SendAsync(pushNotifyRequest);
if (response.IsSuccessStatusCode)
{
inbox.LastWindows8PushNotificationUtc = DateTime.UtcNow;
this.InboxTable.UpdateObject(inbox);
}
else
{
if (failedAttempts == 0)
{
var authHeader = response.Headers.WwwAuthenticate.FirstOrDefault();
if (authHeader != null)
{
if (authHeader.Parameter.Contains("Token expired"))
{
await client.AcquireWnsPushBearerTokenAsync(this.HttpClient);
this.ClientTable.UpdateObject(client);
await this.ClientTable.SaveChangesAsync();
await this.PushNotifyInboxMessageWinStoreAsync(inbox, failedAttempts + 1);
return;
}
}
}
// Log a failure.
// TODO: code here.
}
}