/// <summary>
        /// Synchronously send this message to the destination uri.
        /// </summary>
        /// <param name="payload">The message payload bytes.</param>
        /// <param name="uri">The message destination uri.</param>
        /// <param name="payload">Initialized Web request instance.</param>
        /// <returns>The result instance with relevant information for this send operation.</returns>
        private MessageSendResult SendSynchronously(byte[] payload, Uri uri, HttpWebRequest request)
        {
            try
            {
                // Get the request stream.
                using (var requestStream = request.GetRequestStream())
                {
                    // Start to write the payload to the stream.
                    requestStream.Write(payload, 0, payload.Length);

                    // Switch to receiving the response from MPNS.
                    using (var response = (HttpWebResponse)request.GetResponse())
                    {
                        var result = new MessageSendResult(this, uri, response);
                        if (response.StatusCode != HttpStatusCode.OK)
                        {
                            throw new InvalidOperationException(string.Format(Resources.ServerErrorStatusCode, response.StatusCode));
                        }

                        return(result);
                    }
                }
            }
            catch (WebException ex)
            {
                var result = new MessageSendResult(this, uri, ex);
                throw new MessageSendException(result, ex);
            }
            catch (Exception ex)
            {
                var result = new MessageSendResult(this, uri, ex);
                throw new MessageSendException(result, ex);
            }
        }
 public PushNotificationsLogMsg(NotificationType notificationType, MessageSendResult messageSendResult)
 {
     this.Timestamp = messageSendResult.Timestamp;
     this.MessageId = messageSendResult.AssociatedMessage.Id.ToString();
     this.ChannelUri = messageSendResult.ChannelUri.ToString();
     this.NotificationType = notificationType;
     this.StatusCode = messageSendResult.StatusCode;
     this.NotificationStatus = messageSendResult.NotificationStatus.ToString();
     this.DeviceConnectionStatus = messageSendResult.DeviceConnectionStatus.ToString();
     this.SubscriptionStatus = messageSendResult.DeviceConnectionStatus.ToString();
 }
Пример #3
0
 /// <summary>
 /// Initialize a new instance of this type.
 /// </summary>
 public MessageStatus(string pattern, MessageSendResult result)
 {
     Pattern = pattern;
     Timestamp = result.Timestamp;
     MessageType = MessageTypes[result.AssociatedMessage.GetType()];
     MessageId = result.AssociatedMessage.Id;
     ChannelUri = result.ChannelUri;
     StatusCode = result.StatusCode;
     NotificationStatus = result.NotificationStatus;
     DeviceConnectionStatus = result.DeviceConnectionStatus;
     SubscriptionStatus = result.SubscriptionStatus;
 }
        /// <summary>
        /// Asynchronously send this message to the destination uri using the HttpWebRequest context.
        /// </summary>
        /// <param name="payload">The message payload bytes.</param>
        /// <param name="uri">The message destination uri.</param>
        /// <param name="payload">Initialized Web request instance.</param>
        /// <param name="sent">Message sent callback.</param>
        /// <param name="error">Message send error callback.</param>
        /// <returns>The result instance with relevant information for this send operation.</returns>
        private void SendAsynchronously(byte[] payload, Uri uri, HttpWebRequest request, Action <MessageSendResult> sent, Action <MessageSendResult> error)
        {
            try
            {
                // Get the request stream asynchronously.
                request.BeginGetRequestStream(requestAsyncResult =>
                {
                    try
                    {
                        using (var requestStream = request.EndGetRequestStream(requestAsyncResult))
                        {
                            // Start writing the payload to the stream.
                            requestStream.Write(payload, 0, payload.Length);
                        }

                        // Switch to receiving the response from MPNS asynchronously.
                        request.BeginGetResponse(responseAsyncResult =>
                        {
                            try
                            {
                                using (var response = (HttpWebResponse)request.EndGetResponse(responseAsyncResult))
                                {
                                    var result = new MessageSendResult(this, uri, response);
                                    if (response.StatusCode == HttpStatusCode.OK)
                                    {
                                        sent(result);
                                    }
                                    else
                                    {
                                        error(result);
                                    }
                                }
                            }
                            catch (Exception ex3)
                            {
                                error(new MessageSendResult(this, uri, ex3));
                            }
                        }, null);
                    }
                    catch (Exception ex2)
                    {
                        error(new MessageSendResult(this, uri, ex2));
                    }
                }, null);
            }
            catch (Exception ex1)
            {
                error(new MessageSendResult(this, uri, ex1));
            }
        }
 private void OnMessageSent(NotificationType type, MessageSendResult result)
 {
     PushNotificationsLogMsg msg =
         new PushNotificationsLogMsg(type, result);
     Dispatcher.BeginInvoke((Action)(() =>
     { trace.Add(msg); }));
 }
        private void OnRawSent(string userName, MessageSendResult result)
        {
            // In case that the device is disconnected, no need to send a tile message.
            if (result.DeviceConnectionStatus == DeviceConnectionStatus.TempDisconnected)
            {
                return;
            }

            // Checking these three flags we can know what's the state of both the device and apllication.
            bool isApplicationRunning =
                result.SubscriptionStatus == SubscriptionStatus.Active &&
                result.NotificationStatus == NotificationStatus.Received &&
                result.DeviceConnectionStatus == DeviceConnectionStatus.Connected;

            // In case that the application is not running, send a tile update with counter increase.
            if (!isApplicationRunning)
            {
                var tileMsg = new TilePushNotificationMessage(MessageSendPriority.High)
                {
                    Count = IncreaseCounter(userName),
                    BackgroundImageUri = BackgroundImageUri,
                    Title = Title
                };

                tileMsg.SendAsync(result.ChannelUri, Log, Log);
            }
        }
 /// <summary>
 /// Logs push message result.
 /// </summary>
 protected void Log(MessageSendResult result)
 {
     ResultLogger.Log(DisplayName, result);
 }
 void IMessageSendResultLogger.Log(string patternName, MessageSendResult result)
 {
     Dispatcher.BeginInvoke(() => Status.Add(new MessageStatus(patternName, result)));
 }
 /// <summary>
 /// Initializes a new instance of this type.
 /// </summary>
 /// <param name="result">The send operation result.</param>
 /// <param name="innerException">An inner exception causes this error.</param>
 internal MessageSendException(MessageSendResult result, Exception innerException)
     : base(Resources.FailedToSendMessage, innerException)
 {
     Result = result;
 }
 private bool CheckIfPinned(MessageSendResult result)
 {
     // We known if the application is pinned by checking the following send result flags:
     return result.DeviceConnectionStatus == DeviceConnectionStatus.Connected &&
            result.SubscriptionStatus == SubscriptionStatus.Active &&
            result.NotificationStatus == NotificationStatus.Received;
 }
 /// <summary>
 /// Once tile update sent, check if handled by the phone.
 /// In case that the application is not pinned, ask to pin.
 /// </summary>
 private void OnMessageSent(string userName, MessageSendResult result)
 {
     if (!CheckIfPinned(result))
     {
         AskUserToPin(result.ChannelUri);
     }
 }
 /// <summary>
 /// Initializes a new instance of this type.
 /// </summary>
 /// <param name="result">The send operation result.</param>
 /// <param name="innerException">An inner exception causes this error.</param>
 internal MessageSendException(MessageSendResult result, Exception innerException)
     : base(Resources.FailedToSendMessage, innerException)
 {
     Result = result;
 }