Windows Phone开发(44):推送通知第二集之磁贴通知

移动开发
重点就是,Action="Clear",但要注意,磁贴正面的背景图不能清除。好,再来新建一个WP应用,这回要做客户端。直接新建即可,XAML文档不用改,因为我们不需要界面设计了,直打开后台代码吧。

前面我们说了***个类型——Toast通知,这玩意儿不知大家是不是觉得很新鲜,以前玩.NET编程应该没接触过吧?

其实这东西绝对不复杂,只是刚接触的时候会有点莫名罢了,Toast通知和今天要说的磁贴通知,都有一个共同点,那就是格式都规定死了D。

本质就是向特定的URI地址POST一个XML文档罢了,相信很多人都会,如果你还不会,真的,要补一补基础课了。

多说无益,还是快点切入主题,开门见水吧。

首先,我们要知道我们在服务器端要POST什么样的XML文档,来,一起来看看。

  1. <?xml version="1.0" encoding="utf-8" ?>   
  2. <wp:Notification xmlns:wp="WPNotification">   
  3.   <wp:Tile ID="导航URI">   
  4.     <wp:BackgroundImage>正面背景图片</wp:BackgroundImage>   
  5.     <wp:Count>计数器</wp:Count>   
  6.     <wp:Title>正面标题</wp:Title>   
  7.     <wp:BackBackgroundImage>背面背景图片</wp:BackBackgroundImage>   
  8.     <wp:BackTitle>背面标题</wp:BackTitle>   
  9.     <wp:BackContent>背面内容</wp:BackContent>   
  10.   </wp:Tile>   
  11. </wp:Notification> 

前面关于磁贴的内容,大家有印象吧?

磁帖者,有正面的标题、背景图、计数器;背面有标题、背景图和正文。有印象就好,不用我打水口枪。

来吧,我们通过一个现场演练来体会体会吧。

先做服务器端,这回我选择用ASP.NET,不要告诉我你不会。

启动VS,建一个ASP.NET网站,然后,把default.aspx改造一下,如果你嫌生成的代码不好看,可以把文件删除,然后新建一个页面。

好了,页面布局嘛,我贴一下HTML就行了。

  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>   
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">   
  3. <html xmlns="http://www.w3.org/1999/xhtml">   
  4. <head runat="server">   
  5.     <title></title>   
  6. </head>   
  7. <body>   
  8.     <form id="form1" runat="server">   
  9.     <div>   
  10.         <div>   
  11.             目标URI:   
  12.             <asp:TextBox ID="txtURI" runat="server" Width="911px"></asp:TextBox>   
  13.         </div>   
  14.         <div>   
  15.             <table border="0">   
  16.                 <tr>   
  17.                     <td>正面背景:</td>   
  18.                     <td>   
  19.                         <asp:TextBox ID="txtBackImg" runat="server" Width="316px"></asp:TextBox></td>   
  20.                 </tr>   
  21.                 <tr>   
  22.                     <td>正面标题:</td>   
  23.                     <td>   
  24.                         <asp:TextBox ID="txtTitle" runat="server" Width="316px"></asp:TextBox>   
  25.                     </td>   
  26.                 </tr>   
  27.                 <tr>   
  28.                     <td>计数:</td>   
  29.                     <td>   
  30.                         <asp:TextBox ID="txtCount" runat="server" Width="313px"></asp:TextBox>   
  31.                     </td>   
  32.                 </tr>   
  33.                 <tr>   
  34.                     <td>背面背景:</td>   
  35.                     <td>   
  36.                         <asp:TextBox ID="txtBackBackImg" runat="server" Width="316px"></asp:TextBox>   
  37.                     </td>   
  38.                 </tr>   
  39.                 <tr>   
  40.                     <td>背面标题:</td>   
  41.                     <td>   
  42.                         <asp:TextBox ID="txtBackTitle" runat="server" Width="321px"></asp:TextBox>   
  43.                     </td>   
  44.                 </tr>   
  45.                 <tr>   
  46.                     <td>背面正文:</td>   
  47.                     <td>   
  48.                         <asp:TextBox ID="txtBackContent" runat="server" Width="309px"></asp:TextBox>   
  49.                     </td>   
  50.                 </tr>   
  51.             </table>   
  52.             <div style="margin-left:20px; margin-top:10px;">   
  53.                 <asp:Button ID="btnSend" runat="server" Text="发送" onclick="btnSend_Click" /></div>   
  54.         </div>   
  55.         <div style=" margin-top:20px;">   
  56.             <asp:TextBox ID="txtRes" runat="server" Height="155px" TextMode="MultiLine"    
  57.                 Width="729px"></asp:TextBox>   
  58.         </div>   
  59.     </div>   
  60.     </form>   
  61. </body>   
  62. </html>   

还是别少了后台代码。

  1. /*  
  2.  <?xml version="1.0" encoding="utf-8" ?>  
  3. <wp:Notification xmlns:wp="WPNotification">  
  4.   <wp:Tile ID="导航URI">  
  5.     <wp:BackgroundImage>正面背景图片</wp:BackgroundImage>  
  6.     <wp:Count>计数器</wp:Count>  
  7.     <wp:Title>正面标题</wp:Title>  
  8.     <wp:BackBackgroundImage>背面背景图片</wp:BackBackgroundImage>  
  9.     <wp:BackTitle>背面标题</wp:BackTitle>  
  10.     <wp:BackContent>背面内容</wp:BackContent>  
  11.   </wp:Tile>  
  12. </wp:Notification>  
  13.  * 清除磁贴的属性值  
  14.  <?xml version="1.0" encoding="utf-8" ?>  
  15. <wp:Notification xmlns:wp="WPNotification">  
  16.   <wp:Tile ID="导航URI">  
  17.     <wp:BackgroundImage></wp:BackgroundImage>  
  18.     <wp:Count Action="Clear"></wp:Count>  
  19.     <wp:Title Action="Clear"></wp:Title>  
  20.     <wp:BackBackgroundImage Action="Clear"></wp:BackBackgroundImage>  
  21.     <wp:BackTitle Action="Clear"></wp:BackTitle>  
  22.     <wp:BackContent Action="Clear"></wp:BackContent>  
  23.   </wp:Tile>  
  24. </wp:Notification>  
  25.  * HTTP标头  
  26.  X-WindowsPhone-Target: token  
  27. X-NotificationClass:1  
  28. 1 立即发送  
  29. 11 450秒发送  
  30. 21  900秒发送  
  31.  */   
  32. using System;   
  33. using System.Collections.Generic;   
  34. using System.Linq;   
  35. using System.Web;   
  36. using System.Web.UI;   
  37. using System.Web.UI.WebControls;   
  38. using System.Net;   
  39. using System.Net.Mime;   
  40. using System.IO;   
  41. using System.Text;   
  42. public partial class _Default : System.Web.UI.Page   
  43. {   
  44.     protected void Page_Load(object sender, EventArgs e)   
  45.     {   
  46.     }   
  47.     protected void btnSend_Click(object sender, EventArgs e)   
  48.     {   
  49.         HttpWebRequest request = (HttpWebRequest)WebRequest.Create(txtURI.Text);   
  50.         request.Method = WebRequestMethods.Http.Post;   
  51.         // 加上HTTP标头   
  52.         request.Headers.Add("X-WindowsPhone-Target""token");   
  53.         request.Headers.Add("X-NotificationClass""1");   
  54.         // 拼接内容,XML文档   
  55.         string Msg = "<?xml version=\"1.0\" encoding=\"utf-8\" ?>" +   
  56.                      "<wp:Notification xmlns:wp=\"WPNotification\">" +   
  57.                         "<wp:Tile>" +   
  58.                             "<wp:BackgroundImage>" + txtBackImg.Text + "</wp:BackgroundImage>" +   
  59.                             "<wp:Count>" + txtCount.Text + "</wp:Count>" +   
  60.                             "<wp:Title>" + txtTitle.Text + "</wp:Title>" +   
  61.                             "<wp:BackBackgroundImage>" + txtBackBackImg.Text + "</wp:BackBackgroundImage>" +   
  62.                             "<wp:BackTitle>" + txtBackTitle.Text + "</wp:BackTitle>" +   
  63.                             "<wp:BackContent>" + txtBackContent.Text + "</wp:BackContent>" +   
  64.                         "</wp:Tile>" +   
  65.                       "</wp:Notification>";   
  66.         byte[] buffer = Encoding.UTF8.GetBytes(Msg);   
  67.         request.ContentType = MediaTypeNames.Text.Xml;   
  68.         // POST数据要记得设置内容长度   
  69.         request.ContentLength = buffer.Length;   
  70.         // 写入流   
  71.         using (Stream stream = request.GetRequestStream())   
  72.         {   
  73.             stream.Write(buffer, 0, buffer.Length);   
  74.         }   
  75.         // 接收回应   
  76.         HttpWebResponse response = (HttpWebResponse)request.GetResponse();   
  77.         // 读出响应的HTTP头   
  78.         string headers = "";   
  79.         foreach (string key in response.Headers.AllKeys)   
  80.         {   
  81.             headers += key + " : " + response.Headers.Get(key) + "\r\n";   
  82.         }   
  83.         txtRes.Text = headers;   
  84.     }   
  85. }   

补充一下,上面代码中,前面的注释我已经写上了,其实MSDN上都有,我想很多人不看,我说一下,如果你打算清除磁贴某些属性的值,如标题等,这可以用以下的XML文档。

  1. <?xml version="1.0" encoding="utf-8" ?>   
  2. <wp:Notification xmlns:wp="WPNotification">   
  3.   <wp:Tile ID="导航URI">   
  4.     <wp:BackgroundImage></wp:BackgroundImage>   
  5.     <wp:Count Action="Clear"></wp:Count>   
  6.     <wp:Title Action="Clear"></wp:Title>   
  7.     <wp:BackBackgroundImage Action="Clear"></wp:BackBackgroundImage>   
  8.     <wp:BackTitle Action="Clear"></wp:BackTitle>   
  9.     <wp:BackContent Action="Clear"></wp:BackContent>   
  10.   </wp:Tile>   
  11. </wp:Notification>  

重点就是,Action="Clear",但要注意,磁贴正面的背景图不能清除。

好,再来新建一个WP应用,这回要做客户端。

直接新建即可,XAML文档不用改,因为我们不需要界面设计了,直打开后台代码吧。

  1. using System;   
  2. using System.Collections.Generic;   
  3. using System.Linq;   
  4. using System.Net;   
  5. using System.Windows;   
  6. using System.Windows.Controls;   
  7. using System.Windows.Documents;   
  8. using System.Windows.Input;   
  9. using System.Windows.Media;   
  10. using System.Windows.Media.Animation;   
  11. using System.Windows.Shapes;   
  12. using Microsoft.Phone.Controls;   
  13. using Microsoft.Phone.Notification;   
  14. namespace WPClient   
  15. {   
  16.     public partial class MainPage : PhoneApplicationPage   
  17.     {   
  18.         // 构造函数   
  19.         public MainPage()   
  20.         {   
  21.             InitializeComponent();   
  22.             HttpNotificationChannel Channel = null;   
  23.             // 通道名,随便弄一个,不要与其它应用程序重复   
  24.             string Channel_Name = "TileNoftification";   
  25.             // 在现有的通道里面找找,看能不能找着?   
  26.             Channel = HttpNotificationChannel.Find(Channel_Name);   
  27.             if (Channel == null)   
  28.             {   
  29.                 // 找不到,那就新建一个呗   
  30.                 Channel = new HttpNotificationChannel(Channel_Name);   
  31.                 // 打开通道前要先注册事件处理,为什么?自己想一下吧   
  32.                 // 就是因为ChannelUriUpdated事件,如不这样,   
  33.                 // 当***次获取URI时你就得不到更新通知   
  34.                 Channel.ChannelUriUpdated += new EventHandler<NotificationChannelUriEventArgs>(Channel_ChannelUriUpdated);   
  35.                 // 出事了,总得向上级汇报一下吧?   
  36.                 Channel.ErrorOccurred += new EventHandler<NotificationChannelErrorEventArgs>(Channel_ErrorOccurred);   
  37.                 // 登记注册完毕,着手办喜事   
  38.                 Channel.Open();   
  39.                 // 办喜事别忘记了发请帖啊,调用BindToShellTile   
  40.                 Channel.BindToShellTile();   
  41.             }   
  42.             else   
  43.             {   
  44.                 // 如果找到了通道,还是要注册一下事件   
  45.                 // 老夫妻也可以再拍一回婚纱照吧?   
  46.                 Channel.ChannelUriUpdated+=new EventHandler<NotificationChannelUriEventArgs>(Channel_ChannelUriUpdated);   
  47.                 Channel.ErrorOccurred+=new EventHandler<NotificationChannelErrorEventArgs>(Channel_ErrorOccurred);   
  48.                 // 把住址告诉人家,相册做好之后,数码冲印店送货上门   
  49.                 System.Diagnostics.Debug.WriteLine("URI: {0}", Channel.ChannelUri.ToString());   
  50.             }   
  51.         }   
  52.         void Channel_ErrorOccurred(object sender, NotificationChannelErrorEventArgs e)   
  53.         {   
  54.             // 向上级汇报一下错误   
  55.             Dispatcher.BeginInvoke(() =>   
  56.                 {   
  57.                     MessageBox.Show(e.Message);   
  58.                 });   
  59.         }   
  60.         void Channel_ChannelUriUpdated(object sender, NotificationChannelUriEventArgs e)   
  61.         {   
  62.             // 搬家了记得通知一下大家新住址   
  63.             Dispatcher.BeginInvoke(() =>   
  64.                 {   
  65.                     System.Diagnostics.Debug.WriteLine("URI: {0}", e.ChannelUri.ToString());   
  66.                 });   
  67.         }   
  68.     }   
  69. }   

先动行WP端,当然,同时运行两个都可以了。

在“输出”窗口中,把这个URI复制到服务器端的网页上。

接着,按模拟器的“开始”按钮,来到“开始”屏幕,向右滑动,看到应用程序列表,在本应用程序上长按,从弹出的菜单中选“固定到开始屏幕”.

然后,回到服务器端页面,填好所有参数,点击“发送”。看结果。

都看到效果了?

图片可以自己准备,png格式,173*173,随便用画图工具搞两下就行了,只是为了测试,把图片加到项目后,设置以下属性就行了。

OK,大家照着上面的多练几次,一定有感觉的,我不想讲理论的东西,因为没什么用。

责任编辑:闫佳明 来源: oschina
相关推荐

2013-04-25 15:15:41

Windows PhoWindows PhoWindows Pho

2013-04-25 14:05:20

Windows PhoWindows PhoWindows Pho

2012-08-16 11:31:30

Windows Pho

2013-02-19 09:04:32

Windows 8开发

2021-06-11 07:30:30

并发高并发内存

2013-04-17 11:21:59

Windows PhoWindows Pho

2020-03-27 18:00:37

微软Windows 10操作系统

2022-05-13 14:16:05

云计算

2022-03-18 12:27:21

Android开发者功能

2011-08-03 16:45:09

iPhone APNS 推送通知

2011-04-06 09:33:40

Push动互联网

2021-06-16 09:49:14

Windows 11微软动态磁贴

2018-03-26 21:31:30

深度学习

2013-07-31 13:13:50

Windows PhoMVVM模式

2010-08-01 15:16:41

Android

2011-09-21 17:07:57

WP7

2011-05-04 14:40:57

推送通知iOS

2011-07-18 13:56:19

2013-04-24 13:19:06

Windows Pho动画DoubleAni

2013-04-24 13:31:59

Windows Pho动画之ColorAni
点赞
收藏

51CTO技术栈公众号