【转载】如何发送和接收 Windows Phone 的 Raw 通知

2012/2/9

本主题介绍向 Microsoft 推送通知服务发送 Raw 通知所需的步骤以及如何在 Windows Phone 上运行的应用程序中接收这些通知。Windows Phone 的推送通知概述包含有关 Raw 通知以及如何使用这些通知的信息。

可以在 Windows Phone 的代码示例中找到这个已完成的示例。

重要说明重要说明:

本主题中有关从 Web 服务器上运行的 ASP.NET 网页发送 Raw 通知的一节需要安装完整版本的 Visual Studio 或免费的 Microsoft Visual Web Developer 2010 Express



在本节中,我们创建一个在 Windows Phone 上运行的应用程序,该应用程序创建一个推送通知通道并处理 Raw 通知事件。

重要说明重要说明:

为了简单起见,我们将 Toast 通知 URI 复制并粘贴到发送通知的网页。通常,此 URI 传递给已为应用程序创建的 Web 服务。

创建用来接收 Raw 通知的推送客户端的步骤

  1. 打开 Visual Studio 并创建一个新的应用程序。模板应该为 Silverlight for Windows Phone C# 类别下的“Windows Phone 应用程序”

  2. 将项目命名为 RawNotificationClient

  3. 向 MainPage.xaml.cs 文件的顶部添加以下 using 指令。

    using Microsoft.Phone.Notification;
    
    
    
  4. 用下面的代码替换 MainPage 构造函数。该代码查看在应用程序的早期实例中是否已设置 Raw 通知通道。如果找到通知通道,则通知通道连接到通知事件。如果未找到通知通道,则创建通知通道,然后将其连接到通知事件。

            public MainPage()
            {
                /// Holds the push channel that is created or found.
                HttpNotificationChannel pushChannel;
    
                // The name of our push channel.
                string channelName = "RawSampleChannel";
    
                InitializeComponent();
    
                // Try to find the push channel.
                pushChannel = HttpNotificationChannel.Find(channelName);
    
                // If the channel was not found, then create a new connection to the push service.
                if (pushChannel == null)
                {
                    pushChannel = new HttpNotificationChannel(channelName);
    
                    // Register for all the events before attempting to open the channel.
                    pushChannel.ChannelUriUpdated += new EventHandler<NotificationChannelUriEventArgs>(PushChannel_ChannelUriUpdated);
                    pushChannel.ErrorOccurred += new EventHandler<NotificationChannelErrorEventArgs>(PushChannel_ErrorOccurred);
                    pushChannel.HttpNotificationReceived += new EventHandler<HttpNotificationEventArgs>(PushChannel_HttpNotificationReceived);
    
                    pushChannel.Open();
    
                }
                else
                {
                    // The channel was already open, so just register for all the events.
                    pushChannel.ChannelUriUpdated += new EventHandler<NotificationChannelUriEventArgs>(PushChannel_ChannelUriUpdated);
                    pushChannel.ErrorOccurred += new EventHandler<NotificationChannelErrorEventArgs>(PushChannel_ErrorOccurred);
                    pushChannel.HttpNotificationReceived += new EventHandler<HttpNotificationEventArgs>(PushChannel_HttpNotificationReceived);
    
                    // Display the URI for testing purposes. Normally, the URI would be passed back to your web service at this point.
                    System.Diagnostics.Debug.WriteLine(pushChannel.ChannelUri.ToString());
                    MessageBox.Show(String.Format("Channel Uri is {0}",
                        pushChannel.ChannelUri.ToString()));
    
                }
            }
    
    
    
    
  5. 下面,我们添加事件处理程序的代码。第一个事件处理程序用于 ChannelUriUpdated 事件。为了简单起见,在此处显示通道 URI,但通常此 URI 将发送回 Web 服务。

            void PushChannel_ChannelUriUpdated(object sender, NotificationChannelUriEventArgs e)
            {
    
                Dispatcher.BeginInvoke(() =>
                {
                    // Display the new URI for testing purposes. Normally, the URI would be passed back to your web service at this point.
                    System.Diagnostics.Debug.WriteLine(e.ChannelUri.ToString());
                    MessageBox.Show(String.Format("Channel Uri is {0}",
                        e.ChannelUri.ToString()));
                    
                });
            }
    
    
  6. 下一个事件处理程序用于错误处理。您的代码应该能够正常处理通知错误,因为数据连接可能因手机的位置和服务而异。

            void PushChannel_ErrorOccurred(object sender, NotificationChannelErrorEventArgs e)
            {
                // Error handling logic for your particular application would be here.
                Dispatcher.BeginInvoke(() =>
                    MessageBox.Show(String.Format("A push notification {0} error occurred.  {1} ({2}) {3}",
                        e.ErrorType, e.Message, e.ErrorCode, e.ErrorAdditionalData))
                        );
            }
    
    
  7. 最后一个事件处理程序用于接收您的 Raw 通知。此数据的使用完全与应用程序相关。

               void PushChannel_HttpNotificationReceived(object sender, HttpNotificationEventArgs e)
            {
                string message;
    
                using (System.IO.StreamReader reader = new System.IO.StreamReader(e.Notification.Body))
                {
                    message = reader.ReadToEnd();
                }
    
    
                Dispatcher.BeginInvoke(() =>
                    MessageBox.Show(String.Format("Received Notification {0}:\n{1}",
                        DateTime.Now.ToShortTimeString(), message))
                        );
            }
    
    

Windows Phone 推送客户端代码现在已完成。完成用来发送通知的网页之后,我们将运行此代码。

在本节中,我们创建一个 ASP.NET 网页,该网页使用在设备上创建推送通道时返回的 URI 来发送 Raw 通知。

若要创建 ASP.NET 项目,您需要完整版本的 Visual Studio 或免费的 Microsoft Visual Web Developer 2010 Express

创建用来发送 Raw 通知的 ASP.NET 页面的步骤

  1. 打开 Visual Studio 的另一个实例并创建一个新的应用程序。模板应该为 Web C# 类别下的“ASP.NET Empty Web 应用程序”

  2. 将项目命名为 SendRaw

  3. 通过右键单击“SendRaw”项目名称,然后依次选择“添加”“新项”“Web 表单”向项目中添加一个新的 Web 表单。

  4. 将该表单命名为“SendRaw”,然后单击“添加”按钮。

  5. 通过在“解决方案资源管理器”中右键单击“SendRaw.aspx”,然后选择“设为起始页”使 SendRaw 表单成为起始页。

  6. 下一步是向 SendRaw.aspx Web 表单中添加以下控件。

    控件类型

    控件 ID

    控件的文本

    TextBox

    TextBoxUri

    输入 URI:

    TextBox

    TextBoxValue1

    输入值 1:

    TextBox

    TextBoxValue2

    输入值 2:

    Button

    ButtonSendRaw

    发送 Raw 通知

    TextBox

    TextBoxResponse

    响应:

    “发送 Raw”按钮将拥有 ButtonSendRaw_Click 事件处理程序。将 SendRaw.aspx 的内容替换为以下代码以创建这些控件。

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="SendRaw.aspx.cs" Inherits="SendRaw.SendRaw" %>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head id="Head1" runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
        
            <br />
            Enter URI:</div>
        <asp:TextBox ID="TextBoxUri" runat="server" Width="666px"></asp:TextBox>
        <br />
        <br />
        Enter Value 1:<br />
        <asp:TextBox ID="TextBoxValue1" runat="server"></asp:TextBox>
        <br />
        <br />
        Enter Value 2:<br />
        <asp:TextBox ID="TextBoxValue2" runat="server"></asp:TextBox>
        <br />
        <br />
        <br />
        <asp:Button ID="ButtonSendRaw" runat="server" onclick="ButtonSendRaw_Click" 
            Text="Send Raw Notification" />
        <br />
        <br />
        Response:<br />
        <asp:TextBox ID="TextBoxResponse" runat="server" Height="78px" Width="199px"></asp:TextBox>
        </form>
    </body>
    </html>
    
    
    
  7. 向 SendRaw.aspx.cs 文件的顶部添加以下 using 指令。

    using System.Net;
    using System.IO;
    using System.Text;
    
    
  8. 添加 ButtonSendRaw_Click 事件处理程序的代码。该代码将获取在第一个 TextBox 中输入的 URI,形成 Toast 通知消息,然后将其发布到 Microsoft 推送通知服务。

                 protected void ButtonSendRaw_Click(object sender, EventArgs e)
            {
                try
                {
                    // Get the URI that the Microsoft Push Notification Service returns to the push client when creating a notification channel.
                    // Normally, a web service would listen for URIs coming from the web client and maintain a list of URIs to send
                    // notifications out to.
                    string subscriptionUri = TextBoxUri.Text.ToString();
    
    
                    HttpWebRequest sendNotificationRequest = (HttpWebRequest)WebRequest.Create(subscriptionUri);
    
                    // 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.
                    sendNotificationRequest.Method = "POST";
    
                    // Create the raw message.
                    string rawMessage = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
                    "<root>" +
                        "<Value1>" + TextBoxValue1.Text.ToString() + "<Value1>" +
                        "<Value2>" + TextBoxValue2.Text.ToString() + "<Value2>" +
                    "</root>";
    
                    // Set the notification payload to send.
                    byte[] notificationMessage = Encoding.Default.GetBytes(rawMessage);
    
                    // Set the web request content length.
                    sendNotificationRequest.ContentLength = notificationMessage.Length;
                    sendNotificationRequest.ContentType = "text/xml";
                    sendNotificationRequest.Headers.Add("X-NotificationClass", "3");
    
    
                    using (Stream requestStream = sendNotificationRequest.GetRequestStream())
                    {
                        requestStream.Write(notificationMessage, 0, notificationMessage.Length);
                    }
    
                    // Send the notification and get the response.
                    HttpWebResponse response = (HttpWebResponse)sendNotificationRequest.GetResponse();
                    string notificationStatus = response.Headers["X-NotificationStatus"];
                    string notificationChannelStatus = response.Headers["X-SubscriptionStatus"];
                    string deviceConnectionStatus = response.Headers["X-DeviceConnectionStatus"];
    
                    // Display the response from the Microsoft Push Notification Service.  
                    // Normally, error handling code would be here. In the real world, because data connections are not always available,
                    // notifications may need to be throttled back if the device cannot be reached.
                    TextBoxResponse.Text = notificationStatus + " | " + deviceConnectionStatus + " | " + notificationChannelStatus;
                }
                catch (Exception ex)
                {
                    TextBoxResponse.Text = "Exception caught sending update: " + ex.ToString();
                }
    
            }
    
    
    

若要运行此示例,我们首先运行推送客户端代码以创建通道并获取 URI。然后,我们在网页中使用此 URI 来发送 Raw 通知。

运行示例的步骤

  1. 回到 RawNotificationClient 项目并运行该项目。Windows Phone 模拟器进行初始化,应用程序随后启动。一段时间之后,应用程序应该显示带有推送通道 URI 的消息。此 URI 还显示在 Visual Studio 调试器的“输出”窗口中。

    AP_Push_OutputWindow
    提示提示:

    在 Visual Studio 2010 Express for Windows Phone 中,默认设置是在调试会话期间不显示“输出”窗口。您可以通过转到“调试”菜单,选择“窗口”,然后选择“输出”来显示“输出”窗口。滚动窗口以查找 URI。

  2. 现在,已创建通知通道。将此 URI 从 Visual Studio 调试器“输出”窗口复制到剪贴板。

  3. 切换到“SendRaw”项目并运行该项目。

  4. 在 URI 文本框中,粘贴从上一个项目中复制的 URI。

  5. 为 Raw 通知数据输入一对值。单击“发送 Raw”按钮。

    AP_Push_SendRaw
  6. 在模拟器上运行的 Windows Phone 应用程序中,您应该能够收到 Raw 通知。仅当应用程序运行时才会收到此通知。

    AP_Push_RawEvent

现在,您已经了解如何将 ASP.NET 页面的原始数据传递到正在 Windows Phone 上运行的应用程序。

原文地址:https://www.cnblogs.com/fx2008/p/2471735.html