糖尿病康复,内容丰富有趣,生活中的好帮手!
糖尿病康复 > Windows Phone开发(44):推送通知第二集——磁贴通知

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

时间:2020-11-07 11:50:39

相关推荐

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

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

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

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

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

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

[html]view plaincopyprint? <?xmlversion="1.0"encoding="utf-8"?><wp:Notificationxmlns:wp="WPNotification"><wp:TileID="导航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>

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

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

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

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

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

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

[html]view plaincopyprint? <%@PageLanguage="C#"AutoEventWireup="true"CodeFile="Default.aspx.cs"Inherits="_Default"%><!DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//EN""/TR/xhtml1/DTD/xhtml1-transitional.dtd"><htmlxmlns="/1999/xhtml"><headrunat="server"><title></title></head><body><formid="form1"runat="server"><div><div>目标URI:<asp:TextBoxID="txtURI"runat="server"Width="911px"></asp:TextBox></div><div><tableborder="0"><tr><td>正面背景:</td><td><asp:TextBoxID="txtBackImg"runat="server"Width="316px"></asp:TextBox></td></tr><tr><td>正面标题:</td><td><asp:TextBoxID="txtTitle"runat="server"Width="316px"></asp:TextBox></td></tr><tr><td>计数:</td><td><asp:TextBoxID="txtCount"runat="server"Width="313px"></asp:TextBox></td></tr><tr><td>背面背景:</td><td><asp:TextBoxID="txtBackBackImg"runat="server"Width="316px"></asp:TextBox></td></tr><tr><td>背面标题:</td><td><asp:TextBoxID="txtBackTitle"runat="server"Width="321px"></asp:TextBox></td></tr><tr><td>背面正文:</td><td><asp:TextBoxID="txtBackContent"runat="server"Width="309px"></asp:TextBox></td></tr></table><divstyle="margin-left:20px;margin-top:10px;"><asp:ButtonID="btnSend"runat="server"Text="发送"onclick="btnSend_Click"/></div></div><divstyle="margin-top:20px;"><asp:TextBoxID="txtRes"runat="server"Height="155px"TextMode="MultiLine"Width="729px"></asp:TextBox></div></div></form></body></html>

还是别少了后台代码。

[csharp]view plaincopyprint? /*<?xmlversion="1.0"encoding="utf-8"?><wp:Notificationxmlns:wp="WPNotification"><wp:TileID="导航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>*清除磁贴的属性值<?xmlversion="1.0"encoding="utf-8"?><wp:Notificationxmlns:wp="WPNotification"><wp:TileID="导航URI"><wp:BackgroundImage></wp:BackgroundImage><wp:CountAction="Clear"></wp:Count><wp:TitleAction="Clear"></wp:Title><wp:BackBackgroundImageAction="Clear"></wp:BackBackgroundImage><wp:BackTitleAction="Clear"></wp:BackTitle><wp:BackContentAction="Clear"></wp:BackContent></wp:Tile></wp:Notification>*HTTP标头X-WindowsPhone-Target:tokenX-NotificationClass:11立即发送11450秒发送21900秒发送*/usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Web;usingSystem.Web.UI;usingSystem.Web.UI.WebControls;;.Mime;usingSystem.IO;usingSystem.Text;publicpartialclass_Default:System.Web.UI.Page{protectedvoidPage_Load(objectsender,EventArgse){}protectedvoidbtnSend_Click(objectsender,EventArgse){HttpWebRequestrequest=(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文档stringMsg="<?xmlversion=\"1.0\"encoding=\"utf-8\"?>"+"<wp:Notificationxmlns: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(Streamstream=request.GetRequestStream()){stream.Write(buffer,0,buffer.Length);}//接收回应HttpWebResponseresponse=(HttpWebResponse)request.GetResponse();//读出响应的HTTP头stringheaders="";foreach(stringkeyinresponse.Headers.AllKeys){headers+=key+":"+response.Headers.Get(key)+"\r\n";}txtRes.Text=headers;}}

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

[html]view plaincopyprint? <?xmlversion="1.0"encoding="utf-8"?><wp:Notificationxmlns:wp="WPNotification"><wp:TileID="导航URI"><wp:BackgroundImage></wp:BackgroundImage><wp:CountAction="Clear"></wp:Count><wp:TitleAction="Clear"></wp:Title><wp:BackBackgroundImageAction="Clear"></wp:BackBackgroundImage><wp:BackTitleAction="Clear"></wp:BackTitle><wp:BackContentAction="Clear"></wp:BackContent></wp:Tile></wp:Notification>

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

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

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

[csharp]view plaincopyprint? usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;;usingSystem.Windows;usingSystem.Windows.Controls;usingSystem.Windows.Documents;usingSystem.Windows.Input;usingSystem.Windows.Media;usingSystem.Windows.Media.Animation;usingSystem.Windows.Shapes;usingMicrosoft.Phone.Controls;usingMicrosoft.Phone.Notification;namespaceWPClient{publicpartialclassMainPage:PhoneApplicationPage{//构造函数publicMainPage(){InitializeComponent();HttpNotificationChannelChannel=null;//通道名,随便弄一个,不要与其它应用程序重复stringChannel_Name="TileNoftification";//在现有的通道里面找找,看能不能找着?Channel=HttpNotificationChannel.Find(Channel_Name);if(Channel==null){//找不到,那就新建一个呗Channel=newHttpNotificationChannel(Channel_Name);//打开通道前要先注册事件处理,为什么?自己想一下吧//就是因为ChannelUriUpdated事件,如不这样,//当第一次获取URI时你就得不到更新通知Channel.ChannelUriUpdated+=newEventHandler<NotificationChannelUriEventArgs>(Channel_ChannelUriUpdated);//出事了,总得向上级汇报一下吧?Channel.ErrorOccurred+=newEventHandler<NotificationChannelErrorEventArgs>(Channel_ErrorOccurred);//登记注册完毕,着手办喜事Channel.Open();//办喜事别忘记了发请帖啊,调用BindToShellTileChannel.BindToShellTile();}else{//如果找到了通道,还是要注册一下事件//老夫妻也可以再拍一回婚纱照吧?Channel.ChannelUriUpdated+=newEventHandler<NotificationChannelUriEventArgs>(Channel_ChannelUriUpdated);Channel.ErrorOccurred+=newEventHandler<NotificationChannelErrorEventArgs>(Channel_ErrorOccurred);//把住址告诉人家,相册做好之后,数码冲印店送货上门System.Diagnostics.Debug.WriteLine("URI:{0}",Channel.ChannelUri.ToString());}}voidChannel_ErrorOccurred(objectsender,NotificationChannelErrorEventArgse){//向上级汇报一下错误Dispatcher.BeginInvoke(()=>{MessageBox.Show(e.Message);});}voidChannel_ChannelUriUpdated(objectsender,NotificationChannelUriEventArgse){//搬家了记得通知一下大家新住址Dispatcher.BeginInvoke(()=>{System.Diagnostics.Debug.WriteLine("URI:{0}",e.ChannelUri.ToString());});}}}

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

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

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

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

都看到效果了?

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

如果觉得《Windows Phone开发(44):推送通知第二集——磁贴通知》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。