Windows Phone開發(44):推送通知第二集——磁貼通知

原文: Windows Phone開發(44):推送通知第二集——磁貼通知

前面咱們說了第一個類型——Toast通知,這玩意兒不知你們是否是以爲很新鮮,之前玩.NET編程應該沒接觸過吧?html

其實這東西絕對不復雜,只是剛接觸的時候會有點莫名罷了,Toast通知和今天要說的磁貼通知,都有一個共同點,那就是格式都規定死了D。編程

本質就是向特定的URI地址POST一個XML文檔罷了,相信不少人都會,若是你還不會,真的,要補一補基礎課了。服務器

 多說無益,仍是快點切入主題,開門見水吧。函數

首先,咱們要知道咱們在服務器端要POST什麼樣的XML文檔,來,一塊兒來看看。工具

<?xml version="1.0" encoding="utf-8" ?>
<wp:Notification xmlns:wp="WPNotification">
  <wp:Tile ID="導航URI">
    <wp:BackgroundImage>正面背景圖片</wp:BackgroundImage>
    <wp:Count>計數器</wp:Count>
    <wp:Title>正面標題</wp:Title>
    <wp:BackBackgroundImage>背面背景圖片</wp:BackBackgroundImage>
    <wp:BackTitle>背面標題</wp:BackTitle>
    <wp:BackContent>背面內容</wp:BackContent>
  </wp:Tile>
</wp:Notification>


前面關於磁貼的內容,你們有印象吧?佈局

磁帖者,有正面的標題、背景圖、計數器;背面有標題、背景圖和正文。有印象就好,不用我打水口槍。測試

 

來吧,咱們經過一個現場演練來體會體會吧。網站

 

先作服務器端,這回我選擇用ASP.NET,不要告訴我你不會。spa

啓動VS,建一個ASP.NET網站,而後,把default.aspx改造一下,若是你嫌生成的代碼很差看,能夠把文件刪除,而後新建一個頁面。.net

好了,頁面佈局嘛,我貼一下HTML就好了。

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!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 runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <div>
            目標URI:
            <asp:TextBox ID="txtURI" runat="server" Width="911px"></asp:TextBox>
        </div>
        <div>
            <table border="0">
                <tr>
                    <td>正面背景:</td>
                    <td>
                        <asp:TextBox ID="txtBackImg" runat="server" Width="316px"></asp:TextBox></td>
                </tr>
                <tr>
                    <td>正面標題:</td>
                    <td>
                        <asp:TextBox ID="txtTitle" runat="server" Width="316px"></asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td>計數:</td>
                    <td>
                        <asp:TextBox ID="txtCount" runat="server" Width="313px"></asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td>背面背景:</td>
                    <td>
                        <asp:TextBox ID="txtBackBackImg" runat="server" Width="316px"></asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td>背面標題:</td>
                    <td>
                        <asp:TextBox ID="txtBackTitle" runat="server" Width="321px"></asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td>背面正文:</td>
                    <td>
                        <asp:TextBox ID="txtBackContent" runat="server" Width="309px"></asp:TextBox>
                    </td>
                </tr>
            </table>
            <div style="margin-left:20px; margin-top:10px;">
                <asp:Button ID="btnSend" runat="server" Text="發送" onclick="btnSend_Click" /></div>
        </div>
        <div style=" margin-top:20px;">
            <asp:TextBox ID="txtRes" runat="server" Height="155px" TextMode="MultiLine" 
                Width="729px"></asp:TextBox>
        </div>
    </div>
    </form>
</body>
</html>


 

仍是別少了後臺代碼。

/*
 <?xml version="1.0" encoding="utf-8" ?>
<wp:Notification xmlns:wp="WPNotification">
  <wp:Tile ID="導航URI">
    <wp:BackgroundImage>正面背景圖片</wp:BackgroundImage>
    <wp:Count>計數器</wp:Count>
    <wp:Title>正面標題</wp:Title>
    <wp:BackBackgroundImage>背面背景圖片</wp:BackBackgroundImage>
    <wp:BackTitle>背面標題</wp:BackTitle>
    <wp:BackContent>背面內容</wp:BackContent>
  </wp:Tile>
</wp:Notification>

 * 清除磁貼的屬性值
 <?xml version="1.0" encoding="utf-8" ?>
<wp:Notification xmlns:wp="WPNotification">
  <wp:Tile ID="導航URI">
    <wp:BackgroundImage></wp:BackgroundImage>
    <wp:Count Action="Clear"></wp:Count>
    <wp:Title Action="Clear"></wp:Title>
    <wp:BackBackgroundImage Action="Clear"></wp:BackBackgroundImage>
    <wp:BackTitle Action="Clear"></wp:BackTitle>
    <wp:BackContent Action="Clear"></wp:BackContent>
  </wp:Tile>
</wp:Notification>

 * HTTP標頭
 X-WindowsPhone-Target: token
X-NotificationClass:1
1 當即發送
11 450秒發送
21  900秒發送
 
 */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Net;
using System.Net.Mime;
using System.IO;
using System.Text;


public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void btnSend_Click(object sender, EventArgs e)
    {
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(txtURI.Text);
        request.Method = WebRequestMethods.Http.Post;
        // 加上HTTP標頭
        request.Headers.Add("X-WindowsPhone-Target", "token");
        request.Headers.Add("X-NotificationClass", "1");
        // 拼接內容,XML文檔
        string Msg = "<?xml version=\"1.0\" encoding=\"utf-8\" ?>" +
                     "<wp:Notification xmlns:wp=\"WPNotification\">" +
                        "<wp:Tile>" +
                            "<wp:BackgroundImage>" + txtBackImg.Text + "</wp:BackgroundImage>" +
                            "<wp:Count>" + txtCount.Text + "</wp:Count>" +
                            "<wp:Title>" + txtTitle.Text + "</wp:Title>" +
                            "<wp:BackBackgroundImage>" + txtBackBackImg.Text + "</wp:BackBackgroundImage>" +
                            "<wp:BackTitle>" + txtBackTitle.Text + "</wp:BackTitle>" +
                            "<wp:BackContent>" + txtBackContent.Text + "</wp:BackContent>" +
                        "</wp:Tile>" +
                      "</wp:Notification>";
        byte[] buffer = Encoding.UTF8.GetBytes(Msg);
        request.ContentType = MediaTypeNames.Text.Xml;
        // POST數據要記得設置內容長度
        request.ContentLength = buffer.Length;
        // 寫入流
        using (Stream stream = request.GetRequestStream())
        {
            stream.Write(buffer, 0, buffer.Length);
        }
        // 接收回應
        HttpWebResponse response = (HttpWebResponse)request.GetResponse();
        // 讀出響應的HTTP頭
        string headers = "";
        foreach (string key in response.Headers.AllKeys)
        {
            headers += key + " : " + response.Headers.Get(key) + "\r\n";
        }
        txtRes.Text = headers;
    }
}


 

補充一下,上面代碼中,前面的註釋我已經寫上了,其實MSDN上都有,我想不少人不看,我說一下,若是你打算清除磁貼某些屬性的值,如標題等,這能夠用如下的XML文檔。

<?xml version="1.0" encoding="utf-8" ?>
<wp:Notification xmlns:wp="WPNotification">
  <wp:Tile ID="導航URI">
    <wp:BackgroundImage></wp:BackgroundImage>
    <wp:Count Action="Clear"></wp:Count>
    <wp:Title Action="Clear"></wp:Title>
    <wp:BackBackgroundImage Action="Clear"></wp:BackBackgroundImage>
    <wp:BackTitle Action="Clear"></wp:BackTitle>
    <wp:BackContent Action="Clear"></wp:BackContent>
  </wp:Tile>
</wp:Notification>

重點就是,Action="Clear",但要注意,磁貼正面的背景圖不能清除。

 

好,再來新建一個WP應用,這回要作客戶端。

直接新建便可,XAML文檔不用改,由於咱們不須要界面設計了,直打開後臺代碼吧。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;

using Microsoft.Phone.Notification;

namespace WPClient
{
    public partial class MainPage : PhoneApplicationPage
    {
        // 構造函數
        public MainPage()
        {
            InitializeComponent();
            HttpNotificationChannel Channel = null;
            // 通道名,隨便弄一個,不要與其它應用程序重複
            string Channel_Name = "TileNoftification";
            // 在現有的通道里面找找,看能不能找着?
            Channel = HttpNotificationChannel.Find(Channel_Name);
            if (Channel == null)
            {
                // 找不到,那就新建一個唄
                Channel = new HttpNotificationChannel(Channel_Name);
                // 打開通道前要先註冊事件處理,爲何?本身想一下吧
                // 就是由於ChannelUriUpdated事件,如不這樣,
                // 當第一次獲取URI時你就得不到更新通知
                Channel.ChannelUriUpdated += new EventHandler<NotificationChannelUriEventArgs>(Channel_ChannelUriUpdated);
                // 出事了,總得向上級彙報一下吧?
                Channel.ErrorOccurred += new EventHandler<NotificationChannelErrorEventArgs>(Channel_ErrorOccurred);
                // 登記註冊完畢,着手辦喜事
                Channel.Open();
                // 辦喜事別忘記了發請帖啊,調用BindToShellTile
                Channel.BindToShellTile();
            }
            else
            {
                // 若是找到了通道,仍是要註冊一下事件
                // 老夫妻也能夠再拍一回婚紗照吧?
                Channel.ChannelUriUpdated+=new EventHandler<NotificationChannelUriEventArgs>(Channel_ChannelUriUpdated);
                Channel.ErrorOccurred+=new EventHandler<NotificationChannelErrorEventArgs>(Channel_ErrorOccurred);
                // 把住址告訴人家,相冊作好以後,數碼沖印店送貨上門
                System.Diagnostics.Debug.WriteLine("URI: {0}", Channel.ChannelUri.ToString());
            }
        }

        void Channel_ErrorOccurred(object sender, NotificationChannelErrorEventArgs e)
        {
            // 向上級彙報一下錯誤
            Dispatcher.BeginInvoke(() =>
                {
                    MessageBox.Show(e.Message);
                });
        }

        void Channel_ChannelUriUpdated(object sender, NotificationChannelUriEventArgs e)
        {
            // 搬家了記得通知一下你們新住址
            Dispatcher.BeginInvoke(() =>
                {
                    System.Diagnostics.Debug.WriteLine("URI: {0}", e.ChannelUri.ToString());
                });
        }
    }
}


 

先動行WP端,固然,同時運行兩個均可以了。

在「輸出」窗口中,把這個URI複製到服務器端的網頁上。

 

接着,按模擬器的「開始」按鈕,來到「開始」屏幕,向右滑動,看到應用程序列表,在本應用程序上長按,從彈出的菜單中選「固定到開始屏幕」.

 

而後,回到服務器端頁面,填好全部參數,點擊「發送」。看結果。

 

 

都看到效果了?

圖片能夠本身準備,png格式,173*173,隨便用畫圖工具搞兩下就好了,只是爲了測試,把圖片加到項目後,設置如下屬性就好了。

 

OK,你們照着上面的多練幾回,必定有感受的,我不想講理論的東西,由於沒什麼用。

相關文章
相關標籤/搜索