WindowsNotificationStatus ParseStatus(HttpWebResponse resp, WindowsNotification notification) { var result = new WindowsNotificationStatus() { HttpStatus = HttpStatusCode.OK }; try { if (resp != null) { result.Notification = notification; result.HttpStatus = resp.StatusCode; var wnsDebugTrace = resp.Headers["X-WNS-Debug-Trace"]; var wnsDeviceConnectionStatus = resp.Headers["X-WNS-DeviceConnectionStatus"] ?? "connected"; var wnsErrorDescription = resp.Headers["X-WNS-Error-Description"]; var wnsMsgId = resp.Headers["X-WNS-Msg-ID"]; var wnsNotificationStatus = resp.Headers["X-WNS-NotificationStatus"] ?? ""; result.DebugTrace = wnsDebugTrace; result.ErrorDescription = wnsErrorDescription; result.MessageID = wnsMsgId; if (wnsNotificationStatus.Equals("received", StringComparison.InvariantCultureIgnoreCase)) { result.NotificationStatus = WindowsNotificationSendStatus.Received; } else if (wnsNotificationStatus.Equals("dropped", StringComparison.InvariantCultureIgnoreCase)) { result.NotificationStatus = WindowsNotificationSendStatus.Dropped; } else { result.NotificationStatus = WindowsNotificationSendStatus.ChannelThrottled; } if (wnsDeviceConnectionStatus.Equals("connected", StringComparison.InvariantCultureIgnoreCase)) { result.DeviceConnectionStatus = WindowsDeviceConnectionStatus.Connected; } else if (wnsDeviceConnectionStatus.Equals("tempdisconnected", StringComparison.InvariantCultureIgnoreCase)) { result.DeviceConnectionStatus = WindowsDeviceConnectionStatus.TempDisconnected; } else { result.DeviceConnectionStatus = WindowsDeviceConnectionStatus.Disconnected; } } } catch (Exception ex) { Log.Error("Error parsing notification status: {0}", ex.ToString()); } return(result); }
WindowsNotificationStatus ParseStatus(HttpWebResponse resp, WindowsNotification notification) { var result = new WindowsNotificationStatus(); result.Notification = notification; result.HttpStatus = resp.StatusCode; var wnsDebugTrace = resp.Headers["X-WNS-Debug-Trace"]; var wnsDeviceConnectionStatus = resp.Headers["X-WNS-DeviceConnectionStatus"] ?? "connected"; var wnsErrorDescription = resp.Headers["X-WNS-Error-Description"]; var wnsMsgId = resp.Headers["X-WNS-Msg-ID"]; var wnsNotificationStatus = resp.Headers["X-WNS-NotificationStatus"] ?? ""; var wnsAuthenticate = resp.Headers["WWW-Authenticate"]; result.DebugTrace = wnsDebugTrace; result.ErrorDescription = wnsErrorDescription; result.MessageID = wnsMsgId; if (wnsNotificationStatus.Equals("received", StringComparison.InvariantCultureIgnoreCase)) { result.NotificationStatus = WindowsNotificationSendStatus.Received; } else if (wnsNotificationStatus.Equals("dropped", StringComparison.InvariantCultureIgnoreCase)) { result.NotificationStatus = WindowsNotificationSendStatus.Dropped; } else if (wnsAuthenticate != null && wnsAuthenticate.Contains("Token expired")) { result.NotificationStatus = WindowsNotificationSendStatus.TokenExpired; } else { result.NotificationStatus = WindowsNotificationSendStatus.ChannelThrottled; } if (wnsDeviceConnectionStatus.Equals("connected", StringComparison.InvariantCultureIgnoreCase)) { result.DeviceConnectionStatus = WindowsDeviceConnectionStatus.Connected; } else if (wnsDeviceConnectionStatus.Equals("tempdisconnected", StringComparison.InvariantCultureIgnoreCase)) { result.DeviceConnectionStatus = WindowsDeviceConnectionStatus.TempDisconnected; } else { result.DeviceConnectionStatus = WindowsDeviceConnectionStatus.Disconnected; } return(result); }
WindowsNotificationStatus ParseStatus(HttpWebResponse resp, WindowsNotification notification) { var result = new WindowsNotificationStatus(); result.Notification = notification; result.HttpStatus = resp.StatusCode; var wnsDebugTrace = resp.Headers["X-WNS-Debug-Trace"]; var wnsDeviceConnectionStatus = resp.Headers["X-WNS-DeviceConnectionStatus"] ?? "connected"; var wnsErrorDescription = resp.Headers["X-WNS-Error-Description"]; var wnsMsgId = resp.Headers["X-WNS-Msg-ID"]; var wnsNotificationStatus = resp.Headers["X-WNS-NotificationStatus"] ?? ""; result.DebugTrace = wnsDebugTrace; result.ErrorDescription = wnsErrorDescription; result.MessageID = wnsMsgId; if (wnsNotificationStatus.Equals("received", StringComparison.InvariantCultureIgnoreCase)) result.NotificationStatus = WindowsNotificationSendStatus.Received; else if (wnsNotificationStatus.Equals("dropped", StringComparison.InvariantCultureIgnoreCase)) result.NotificationStatus = WindowsNotificationSendStatus.Dropped; else result.NotificationStatus = WindowsNotificationSendStatus.ChannelThrottled; if (wnsDeviceConnectionStatus.Equals("connected", StringComparison.InvariantCultureIgnoreCase)) result.DeviceConnectionStatus = WindowsDeviceConnectionStatus.Connected; else if (wnsDeviceConnectionStatus.Equals("tempdisconnected", StringComparison.InvariantCultureIgnoreCase)) result.DeviceConnectionStatus = WindowsDeviceConnectionStatus.TempDisconnected; else result.DeviceConnectionStatus = WindowsDeviceConnectionStatus.Disconnected; return result; }
void transport_UnhandledException(WindowsNotification notification, Exception exception) { this.Events.RaiseNotificationSendFailure(notification, exception); this.Events.RaiseChannelException(exception, notification); Interlocked.Decrement(ref waitCounter); }
// (INotification notification, SendNotificationCallbackDelegate callback) public void Send(WindowsNotification winNotification, string packageName, string packageSecurityIdentifier, string clientSecret) { //See if we need an access token if (string.IsNullOrEmpty(AccessToken)) { RenewAccessToken(packageSecurityIdentifier, clientSecret); } //https://cloud.notify.windows.com/?token=..... //Authorization: Bearer {AccessToken} // string wnsType, contentType, tag; Win8Notifications.GetHeaders(winNotification, out wnsType, out contentType, out tag); var request = (HttpWebRequest)HttpWebRequest.Create(winNotification.ChannelUri); // "https://notify.windows.com"); request.Method = "POST"; request.Headers.Add("X-WNS-Type", wnsType); request.Headers.Add("Authorization", string.Format("Bearer {0}", this.AccessToken)); request.ContentType = contentType; if (!string.IsNullOrEmpty(tag)) { request.Headers.Add("X-WNS-Tag", tag); } //Microsoft recommends we disable expect-100 to improve latency request.ServicePoint.Expect100Continue = false; var payload = winNotification.GetPayload(); var data = Encoding.UTF8.GetBytes(payload); request.ContentLength = data.Length; using (var rs = request.GetRequestStream()) rs.Write(data, 0, data.Length); try { request.BeginGetResponse(new AsyncCallback(getResponseCallback), new object[] { request, winNotification }); } catch (WebException wex) { //Handle different httpstatuses var status = ParseStatus(wex.Response as HttpWebResponse, winNotification); // If token expired, reset AccessToken in order to obtain a new one if (status.NotificationStatus == WindowsNotificationSendStatus.TokenExpired) { AccessToken = null; } HandleStatus(status); } catch (Exception ex) { UnhandledException(winNotification, ex); } }