【Windows Phone 8】結合ASP.NET進行推播

【Windows Phone 8】結合ASP.NET進行推播

前言:

在開發手機應用程式中有時需要用到推播種功能,

於是在網路上找到這一篇有關如何進行推播的文章,

以下開始實作。

參考:【Notifications for Windows Phone

 

實作:

 

在【方案總管】>【WMAppManifest.xml】>【功能】找到【ID_CAP_PUSH_NOTIFICATION】將它啟用

 

【Phone端】

Step-1 【MainPage】建構式中建立通知通道

 

//尋找有無此通知通道
HttpNotificationChannel push = HttpNotificationChannel.Find("RawSampleChannel");
if (push == null)
{
     push = new HttpNotificationChannel("RawSampleChannel");
    //回傳通知事件
     push.ChannelUriUpdated += push_ChannelUriUpdated;
    //回傳錯誤
    push.ErrorOccurred += push_ErrorOccurred;
    //回傳通知未處理
    push.HttpNotificationReceived += push_HttpNotificationReceived;
   //開啟通知通道
   push.Open();
}
else
{
    //回傳通知事件
    push.ChannelUriUpdated += push_ChannelUriUpdated;
    //回傳錯誤
    push.ErrorOccurred += push_ErrorOccurred;
    //回傳通知未處理
    push.HttpNotificationReceived += push_HttpNotificationReceived;
    //將通道的URI顯示在輸出與MessageBox中
    System.Diagnostics.Debug.WriteLine(push.ChannelUri.ToString());
    MessageBox.Show(String.Format("{0}", push.ChannelUri.ToString()));
}
 
{
    Dispatcher.BeginInvoke(() => {
      //將通道的URI顯示在輸出與MessageBox中
       System.Diagnostics.Debug.WriteLine(e.ChannelUri.ToString());
       MessageBox.Show(String.Format("{0}", e.ChannelUri.ToString()));
   });
}
 
 
{
   //回傳錯誤
   Dispatcher.BeginInvoke(() => {
       MessageBox.Show(String.Format("{0} {1} {2} {3}",e.ErrorType,e.Message,e.ErrorCode,e.ErrorAdditionalData));
   });
}
 


 

{
    string message;
    //回傳通知未處理
    using(System.IO.StreamReader sr=new System.IO.StreamReader(e.Notification.Body))
    {
        message = sr.ReadToEnd();
    }
    Dispatcher.BeginInvoke(() => {
        MessageBox.Show(String.Format("{0} {1}", DateTime.Now.ToShortTimeString(), message));
    });
}
 
    <form id="form1" runat="server">
    <div>
    <br />
    通知通道URI:</div>
    <asp:TextBox ID="TextBoxUri" runat="server" Width="666px"></asp:TextBox>
    <br />
    顯示1:<br />
    <asp:TextBox ID="TextBoxValue1" runat="server"></asp:TextBox>
    <br />
    顯示2:<br />
    <asp:TextBox ID="TextBoxValue2" runat="server"></asp:TextBox>
    <br />
    <asp:Button ID="ButtonSendRaw" runat="server" onclick="ButtonSendRaw_Click" Text="Send Raw Notification" />
    <br />
    Response Error:<br />
    <asp:TextBox ID="TextBoxResponse" runat="server" Height="78px" Width="199px"></asp:TextBox>
    </form>
</body>

Step-2 發送推播事件(以Button觸發)

 

{
    try
    {
        //將Phone的通知通道URI儲存在subscriptionUri變數中
        string subscriptionUri = TextBoxUri.Text.ToString();
        //建立Request(要求)
        HttpWebRequest sendNotificationRequest = (HttpWebRequest)WebRequest.Create(subscriptionUri);

        //Request(要求)方法以POST方式
        sendNotificationRequest.Method = "POST";

        //回傳message.
        string rawMessage = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
        "<root>" +
            "<Value1>" + TextBoxValue1.Text.ToString() + "<Value1>" +
            "<Value2>" + TextBoxValue2.Text.ToString() + "<Value2>" +
        "</root>";

        //以UTF8編碼,位元組方式回傳
        byte[] notificationMessage = Encoding.UTF8.GetBytes(rawMessage);

        //將Request(要求)ContentLength標頭設為notificationMessage長度
        sendNotificationRequest.ContentLength = notificationMessage.Length;
        //設定Request(要求)ContentType為Xml
        sendNotificationRequest.ContentType = "text/xml";
        //將Request(要求)Headers名稱設為X-NotificationClass、值為3
        sendNotificationRequest.Headers.Add("X-NotificationClass", "3");


        using (Stream requestStream = sendNotificationRequest.GetRequestStream())
        {
            //將notificationMessage位元組複製到requestStream資料流中
            //第二個參數為起始,第三個參數為結束
            requestStream.Write(notificationMessage, 0, notificationMessage.Length);
        }

        //建立Response
        HttpWebResponse response = (HttpWebResponse)sendNotificationRequest.GetResponse();
        TextBoxResponse.Text = "送出通知";
    }
    catch (Exception ex)
    {
        TextBoxResponse.Text = "Exception caught sending update: " + ex.ToString();
    }

}
URI
WEB
WEBShow
PhoneShow