Ejemplo n.º 1
0
        /// <summary>
        /// Represents the worker method to send the callback registration request.
        /// </summary>
        /// <param name="windowsPhoneCallbackRegistrationRequest">The callback registration request that associates a callback URI and message with a subscription.</param>
        /// <returns>The response of the Windows Phone Callback Registration Request.</returns>
        public static WindowsPhoneCallbackRegistrationResponse RegisterCallback(WindowsPhoneCallbackRegistrationRequest windowsPhoneCallbackRegistrationRequest)
        {
            var windowsPhoneCallbackRegistrationResponse = new WindowsPhoneCallbackRegistrationResponse()
            {
                OperationStartUtcDateTime = DateTime.UtcNow,
            };

            try
            {
                windowsPhoneCallbackRegistrationResponse.ValidationResults = Validator.Validate(windowsPhoneCallbackRegistrationRequest);

                if (windowsPhoneCallbackRegistrationResponse.ValidationResults.IsValidWithWarnings)
                {
                    var windowsPhoneCallbackRegistrationWebRequest = (HttpWebRequest)WebRequest.Create(windowsPhoneCallbackRegistrationRequest.NotificationUri);

                    windowsPhoneCallbackRegistrationWebRequest.Headers.Add("X-CallbackURI", windowsPhoneCallbackRegistrationRequest.CallbackUri);
                    windowsPhoneCallbackRegistrationWebRequest.Method = "POST";

                    windowsPhoneCallbackRegistrationWebRequest.ContentType   = "application/*";
                    windowsPhoneCallbackRegistrationWebRequest.ContentLength = windowsPhoneCallbackRegistrationRequest.CallbackMessage.Length;

                    using (var notificationRequestStream = windowsPhoneCallbackRegistrationWebRequest.GetRequestStream())
                    {
                        notificationRequestStream.Write(windowsPhoneCallbackRegistrationRequest.CallbackMessage, 0, windowsPhoneCallbackRegistrationRequest.CallbackMessage.Length);
                    }

                    using (var windowsPhoneCallbackRegistrationWebResponse = (HttpWebResponse)windowsPhoneCallbackRegistrationWebRequest.GetResponse())
                    {
                        PushNotifier.SetCallbackRequestWebResponseValuesToResult(windowsPhoneCallbackRegistrationResponse, windowsPhoneCallbackRegistrationWebResponse);
                    }
                }
            }
            catch (Exception exception)
            {
                windowsPhoneCallbackRegistrationResponse.OperationResult = ResultType.Failed;
                windowsPhoneCallbackRegistrationResponse.RawException    = exception;

                var webException = exception as WebException;

                if (webException != null && webException.Response != null)
                {
                    var httpWebResponse = webException.Response as HttpWebResponse;

                    if (httpWebResponse != null)
                    {
                        PushNotifier.SetCallbackRequestWebResponseValuesToResult(windowsPhoneCallbackRegistrationResponse, httpWebResponse);
                    }
                }
            }
            finally
            {
                windowsPhoneCallbackRegistrationResponse.OperationEndUtcDateTime = DateTime.UtcNow;
            }

            return(windowsPhoneCallbackRegistrationResponse);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Represents the worker method to send the actual notification message.
        /// </summary>
        /// <typeparam name="T">Type of <see cref="PushNotificationMessage"/>.</typeparam>
        /// <param name="pushNotificationMessage">The notification message to send.</param>
        /// <returns>The response of the Notification Send operation.</returns>
        public static PushNotificationSendResult SendPushNotificationMessage <T>(T pushNotificationMessage) where T : PushNotificationMessage
        {
            var pushNotificationSendResult = new PushNotificationSendResult()
            {
                OperationStartUtcDateTime = DateTime.UtcNow,
            };

            try
            {
                if (pushNotificationMessage != null)
                {
                    pushNotificationSendResult.DeviceType           = pushNotificationMessage.DeviceType;
                    pushNotificationSendResult.PushNotificationType = pushNotificationMessage.PushNotificationType;
                }

                pushNotificationSendResult.ValidationResults = Validator.Validate(pushNotificationMessage);

                if (pushNotificationSendResult.ValidationResults.IsValidWithWarnings)
                {
                    switch (pushNotificationMessage.DeviceType)
                    {
                    case DeviceType.WindowsPhone7_1:
                    case DeviceType.WindowsPhone7_0:

                        var windowsPhonePushNotificationMessage = pushNotificationMessage as WindowsPhonePushNotificationMessage;

                        var pushNotificationWebRequest = (HttpWebRequest)WebRequest.Create(windowsPhonePushNotificationMessage.NotificationUri);

                        pushNotificationWebRequest.Method      = "POST";
                        pushNotificationWebRequest.ContentType = "text/xml";

                        var messageId = Guid.NewGuid();

                        pushNotificationSendResult.MessageId = messageId;
                        pushNotificationWebRequest.Headers.Add("X-MessageID", messageId.ToString());

                        var windowsPhoneTargetHeader = windowsPhonePushNotificationMessage.PushNotificationType.ToWindowsPhoneTarget();
                        pushNotificationWebRequest.Headers["X-WindowsPhone-Target"] = windowsPhoneTargetHeader;

                        var notificationClass = windowsPhonePushNotificationMessage.MessagePriority.ToNotificationClass(windowsPhonePushNotificationMessage.PushNotificationType);
                        pushNotificationWebRequest.Headers.Add("X-NotificationClass", notificationClass);

                        var notificationPayload = windowsPhonePushNotificationMessage.CreatePayload();
                        pushNotificationWebRequest.ContentLength = notificationPayload.Length;

                        if (windowsPhonePushNotificationMessage.AuthenticationSettings != null && windowsPhonePushNotificationMessage.AuthenticationSettings.EnableAuthentication)
                        {
                            pushNotificationWebRequest.ClientCertificates.Add(windowsPhonePushNotificationMessage.AuthenticationSettings.X509Certificate);
                        }

                        using (var notificationRequestStream = pushNotificationWebRequest.GetRequestStream())
                        {
                            notificationRequestStream.Write(notificationPayload, 0, notificationPayload.Length);
                        }

                        using (var pushNotificationWebResponse = (HttpWebResponse)pushNotificationWebRequest.GetResponse())
                        {
                            PushNotifier.SetNotificationWebResponseValuesToResult(pushNotificationSendResult, pushNotificationWebResponse);
                        }

                        break;
                    }
                }
            }
            catch (Exception exception)
            {
                pushNotificationSendResult.OperationResult = ResultType.Failed;
                pushNotificationSendResult.RawException    = exception;

                var webException = exception as WebException;

                if (webException != null && webException.Response != null)
                {
                    var httpWebResponse = webException.Response as HttpWebResponse;

                    if (httpWebResponse != null)
                    {
                        PushNotifier.SetNotificationWebResponseValuesToResult(pushNotificationSendResult, httpWebResponse);
                    }
                }
            }
            finally
            {
                pushNotificationSendResult.OperationEndUtcDateTime = DateTime.UtcNow;
            }

            return(pushNotificationSendResult);
        }