private async Task PushNotifyInboxMessageWinPhoneAsync(InboxEntity inbox) { if (!string.IsNullOrEmpty(inbox.WinPhone8PushChannelUri)) { var notifications = new WinPhonePushNotifications(this.HttpClient, new Uri(inbox.WinPhone8PushChannelUri)); int count = await this.RetrieveInboxItemsCountAsync(inbox.RowKey); bool invalidChannel = false; try { var pushTile = notifications.PushWinPhoneTileAsync(inbox.WinPhone8TileTemplate, count: count); Task <bool> pushToast = Task.FromResult(false); if (!string.IsNullOrEmpty(inbox.WinPhone8ToastText1) || !string.IsNullOrEmpty(inbox.WinPhone8ToastText2)) { var line1 = string.Format(CultureInfo.InvariantCulture, inbox.WinPhone8ToastText1 ?? string.Empty, count); var line2 = string.Format(CultureInfo.InvariantCulture, inbox.WinPhone8ToastText2 ?? string.Empty, count); if (inbox.LastWinPhone8PushNotificationUtc.HasValue && inbox.LastAuthenticatedInteractionUtc.HasValue && inbox.LastWinPhone8PushNotificationUtc.Value > inbox.LastAuthenticatedInteractionUtc.Value) { // We've sent a toast notification more recently than the user has checked messages, // so there's no reason to send another for now. pushToast = Task.FromResult(true); } else { pushToast = notifications.PushWinPhoneToastAsync(line1, line2); } } Task <bool> pushRaw = Task.FromResult(false); if (!string.IsNullOrEmpty(inbox.WinPhone8PushChannelContent)) { pushRaw = notifications.PushWinPhoneRawNotificationAsync(inbox.WinPhone8PushChannelContent); } await Task.WhenAll(pushTile, pushToast, pushRaw); invalidChannel |= !(pushTile.Result || pushToast.Result || pushRaw.Result); } catch (HttpRequestException) { invalidChannel = true; } if (invalidChannel) { inbox.WinPhone8PushChannelUri = null; inbox.WinPhone8PushChannelContent = null; inbox.WinPhone8ToastText1 = null; inbox.WinPhone8ToastText2 = null; } else { inbox.LastWinPhone8PushNotificationUtc = DateTime.UtcNow; } this.InboxTable.UpdateObject(inbox); } }
private async Task PushNotifyInboxMessageWinPhoneAsync(InboxEntity inbox) { if (!string.IsNullOrEmpty(inbox.WinPhone8PushChannelUri)) { var notifications = new WinPhonePushNotifications(this.HttpClient, new Uri(inbox.WinPhone8PushChannelUri)); int count = await this.RetrieveInboxItemsCountAsync(inbox.RowKey); bool invalidChannel = false; try { var pushTile = notifications.PushWinPhoneTileAsync(inbox.WinPhone8TileTemplate, count: count); Task<bool> pushToast = Task.FromResult(false); if (!string.IsNullOrEmpty(inbox.WinPhone8ToastText1) || !string.IsNullOrEmpty(inbox.WinPhone8ToastText2)) { var line1 = string.Format(CultureInfo.InvariantCulture, inbox.WinPhone8ToastText1 ?? string.Empty, count); var line2 = string.Format(CultureInfo.InvariantCulture, inbox.WinPhone8ToastText2 ?? string.Empty, count); if (inbox.LastWinPhone8PushNotificationUtc.HasValue && inbox.LastAuthenticatedInteractionUtc.HasValue && inbox.LastWinPhone8PushNotificationUtc.Value > inbox.LastAuthenticatedInteractionUtc.Value) { // We've sent a toast notification more recently than the user has checked messages, // so there's no reason to send another for now. pushToast = Task.FromResult(true); } else { pushToast = notifications.PushWinPhoneToastAsync(line1, line2); } } Task<bool> pushRaw = Task.FromResult(false); if (!string.IsNullOrEmpty(inbox.WinPhone8PushChannelContent)) { pushRaw = notifications.PushWinPhoneRawNotificationAsync(inbox.WinPhone8PushChannelContent); } await Task.WhenAll(pushTile, pushToast, pushRaw); invalidChannel |= !(pushTile.Result || pushToast.Result || pushRaw.Result); } catch (HttpRequestException) { invalidChannel = true; } if (invalidChannel) { inbox.WinPhone8PushChannelUri = null; inbox.WinPhone8PushChannelContent = null; inbox.WinPhone8ToastText1 = null; inbox.WinPhone8ToastText2 = null; } else { inbox.LastWinPhone8PushNotificationUtc = DateTime.UtcNow; } this.InboxTable.UpdateObject(inbox); } }