/// <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);
        }
        /// <summary>
        ///  Represents the worker method to send the callback registration request in an async manner.
        /// </summary>
        /// <param name="windowsPhoneCallbackRegistrationRequest">The callback registration request that associates a callback URI and message with a subscription.</param>
        /// <param name="callback">The callback delegate to call once the async operation completes.</param>
        public static void RegisterCallbackAsync(WindowsPhoneCallbackRegistrationRequest windowsPhoneCallbackRegistrationRequest, Action <WindowsPhoneCallbackRegistrationResponse> callback)
        {
            Func <WindowsPhoneCallbackRegistrationRequest, WindowsPhoneCallbackRegistrationResponse> method = PushNotifier.RegisterCallback;

            method.BeginInvoke(
                windowsPhoneCallbackRegistrationRequest,
                (asyncResult) =>
            {
                var m = asyncResult.AsyncState as Func <WindowsPhoneCallbackRegistrationRequest, WindowsPhoneCallbackRegistrationResponse>;

                var result = m.EndInvoke(asyncResult);
                callback(result);
            },
                method);
        }
Exemple #3
0
        /// <summary>
        /// Validates the notification messages for the rules set forth by the Notification System. This may not guarantee that each and every rule will be picked up. e.g. the total message size is not done based on individual fields.
        /// But the basic ruls will be checked per field. This will eliminate a lot of trivial issues.
        /// </summary>
        /// <param name="windowsPhoneCallbackRegistrationRequest">The request of type <see cref="WindowsPhoneCallbackRegistrationRequest" />.</param>
        /// <returns>The list of Validation Messages.</returns>
        public static ValidationResults Validate(WindowsPhoneCallbackRegistrationRequest windowsPhoneCallbackRegistrationRequest)
        {
            var vrs = new ValidationResults();

            if (windowsPhoneCallbackRegistrationRequest == null)
            {
                vrs.Add(new ValidationResult(StringResources.Code_0012));
                return(vrs);
            }

            if (windowsPhoneCallbackRegistrationRequest.EnableValidation)
            {
                if (String.IsNullOrWhiteSpace(windowsPhoneCallbackRegistrationRequest.NotificationUri))
                {
                    vrs.Add(new ValidationResult(StringResources.Code_0010));
                }

                if (String.IsNullOrWhiteSpace(windowsPhoneCallbackRegistrationRequest.CallbackUri))
                {
                    vrs.Add(new ValidationResult(StringResources.Code_0013));
                }

                if (windowsPhoneCallbackRegistrationRequest.CallbackMessage == null)
                {
                    vrs.Add(new ValidationResult(StringResources.Code_0014));
                }
                else
                {
                    if (windowsPhoneCallbackRegistrationRequest.CallbackMessage.Length > 1024)
                    {
                        vrs.Add(new ValidationResult(StringResources.Code_0015));
                    }
                }
            }

            return(vrs);
        }
        private static WindowsPhoneCallbackRegistrationRequest GetValidWindowsPhoneCallbackRegistrationRequest()
        {
            var request = new WindowsPhoneCallbackRegistrationRequest()
            {
                CallbackUri = "http://easynotification.codeplex.com/",
                NotificationUri = WindowsPhonePushNotificationMessageUnitTest.ChannelUri,
            };

            request.CallbackMessage = System.Text.Encoding.Unicode.GetBytes("CallbackMessagePayload");

            return request;
        }