Example #1
0
        /// <summary>
        /// Simulate an incoming call
        /// </summary>
        public void Simulate()
        {
            // Send a push notification to the push channel URI of this app to simulate an incoming call
            try
            {
                // Create an HTTPWebRequest that posts the raw notification to the Microsoft Push Notification Service.
                // HTTP POST is the only method allowed to send the notification.
                HttpWebRequest sendNotificationRequest = (HttpWebRequest)WebRequest.Create(((App)App.Current).PushChannelUri);
                sendNotificationRequest.Method = "POST";

                // Create the raw message.
                byte[] notificationMessage = null;
                using (MemoryStream ms = new MemoryStream())
                {
                    Agents.Notification notification = new Agents.Notification();
                    notification.Name   = this.CallerName;
                    notification.Number = this.CallerNumber;

                    XmlSerializer xs = new XmlSerializer(typeof(Agents.Notification));
                    xs.Serialize(ms, notification);

                    notificationMessage = ms.ToArray();
                }

                // Set the required web request headers
                sendNotificationRequest.ContentLength = notificationMessage.Length;
                sendNotificationRequest.ContentType   = "text/xml";
                sendNotificationRequest.Headers["X-NotificationClass"] = "4"; // Class 4 indicates an incoming VoIP call

                // Write the request body
                sendNotificationRequest.BeginGetRequestStream((IAsyncResult arRequest) =>
                {
                    try
                    {
                        using (Stream requestStream = sendNotificationRequest.EndGetRequestStream(arRequest))
                        {
                            requestStream.Write(notificationMessage, 0, notificationMessage.Length);
                        }

                        // Get the response.
                        sendNotificationRequest.BeginGetResponse((IAsyncResult arResponse) =>
                        {
                            try
                            {
                                HttpWebResponse response      = (HttpWebResponse)sendNotificationRequest.EndGetResponse(arResponse);
                                string notificationStatus     = response.Headers["X-NotificationStatus"];
                                string subscriptionStatus     = response.Headers["X-SubscriptionStatus"];
                                string deviceConnectionStatus = response.Headers["X-DeviceConnectionStatus"];

                                // The push notification was sent
                                this.ShowResult(string.Format("Notification: {0}\r\nSubscription: {1}\r\nDevice: {2}", notificationStatus, subscriptionStatus, deviceConnectionStatus));
                            }
                            catch (Exception ex)
                            {
                                this.ShowResult(ex);
                            }
                        }, null);
                    }
                    catch (Exception ex)
                    {
                        this.ShowResult(ex);
                    }
                }, null);
            }
            catch (Exception ex)
            {
                this.ShowResult(ex);
            }
        }
        /// <summary>
        /// Simulate an incoming call
        /// </summary>
        public void Simulate()
        {
            // Send a push notification to the push channel URI of this app to simulate an incoming call
            try
            {
                // Create an HTTPWebRequest that posts the raw notification to the Microsoft Push Notification Service.
                // HTTP POST is the only method allowed to send the notification.
                HttpWebRequest sendNotificationRequest = (HttpWebRequest)WebRequest.Create(((App)App.Current).PushChannelUri);
                sendNotificationRequest.Method = "POST";

                // Create the raw message.
                byte[] notificationMessage = null;
                using (MemoryStream ms = new MemoryStream())
                {
                    Agents.Notification notification = new Agents.Notification();
                    notification.Name = this.CallerName;
                    notification.Number = this.CallerNumber;

                    XmlSerializer xs = new XmlSerializer(typeof(Agents.Notification));
                    xs.Serialize(ms, notification);

                    notificationMessage = ms.ToArray();
                }

                // Set the required web request headers
                sendNotificationRequest.ContentLength = notificationMessage.Length;
                sendNotificationRequest.ContentType = "text/xml";
                sendNotificationRequest.Headers["X-NotificationClass"] = "4"; // Class 4 indicates an incoming VoIP call

                // Write the request body
                sendNotificationRequest.BeginGetRequestStream((IAsyncResult arRequest) =>
                {
                    try
                    {
                        using (Stream requestStream = sendNotificationRequest.EndGetRequestStream(arRequest))
                        {
                            requestStream.Write(notificationMessage, 0, notificationMessage.Length);
                        }

                        // Get the response.
                        sendNotificationRequest.BeginGetResponse((IAsyncResult arResponse) =>
                        {
                            try
                            {
                                HttpWebResponse response = (HttpWebResponse)sendNotificationRequest.EndGetResponse(arResponse);
                                string notificationStatus = response.Headers["X-NotificationStatus"];
                                string subscriptionStatus = response.Headers["X-SubscriptionStatus"];
                                string deviceConnectionStatus = response.Headers["X-DeviceConnectionStatus"];

                                // The push notification was sent
                                this.ShowResult(string.Format("Notification: {0}\r\nSubscription: {1}\r\nDevice: {2}", notificationStatus, subscriptionStatus, deviceConnectionStatus));
                            }
                            catch (Exception ex)
                            {
                                this.ShowResult(ex);
                            }
                        }, null);
                    }
                    catch (Exception ex)
                    {
                        this.ShowResult(ex);
                    }
                }, null);
            }
            catch (Exception ex)
            {
                this.ShowResult(ex);
            }
        }