Windows Phone开发学习指南
本文和大家一起学习一下Windows Phone开发的概念,PushNotification是windowsphone7中的特色功能之一,它为手机端应用和webservice之间建立了一条专用的、持久的、稳定的通道来推送通知。当通道建立后,手机端应用可以接收webservice的任何信息。一起学Windows Phone开发
一.简介
PushNotification是windowsphone7中的特色功能之一,这个功能可以变相的让普通开发者实现多任务(尽管并不是真正的多任务)。它为手机端应用和webservice之间建立了一条专用的、持久的、稳定的通道来推送通知。当通道建立后,手机端应用可以接收webservice的任何信息。
http://images.51cto.com/files/uploadimg/20100721/1539040.jpg
二.分类
对于PushNotification主要有三种:
1.TileNotification:
是可以改变QuickLanucharea内的图标内容(图片,文字等)的方式。不过这个需要把程序pintostart,才可以使用。
2.ToastNotification:
是在屏幕上面可以显示一个提示栏的方式。当点击提示栏可以打开应用程序。
3.RawNotification:
是直接使用Http方式来接收(httppolling)通知的方式。并且是不可见的,以后台方式传送通知。
对于以上几种通知,都需要一个服务端以pushnotification方式来发送通知,也就是说要使用pushnotification都需要一个服务端。
三.Windows Phone开发中创建服务器端
对于服务器端来说,发送不同的通知,都是以Http方式发出去的,但是在发送时,需要配置相应的参数,来告诉PushNotificationService所发送的类型是什么。
HttpWebRequestrequest=(HttpWebRequest)WebRequest.Create(channelUri);
request.Method=WebRequestMethods.Http.Post;
request.ContentType="text/xml;charset=utf-8";
request.ContentLength=notificationmessage.Length;
request.Headers["X-MessageID"]=Guid.NewGuid().ToString();
1.Toastnotification:
request.Headers["X-WindowsPhone-Target"]="toast";
request.Headers
Message:
[*]"Content-Type:text/xml\r\nX-WindowsPhone-Target:token\r\n\r\n"
[*]
[*]<?xmlversionxmlversion=&quot;1.0&quot;encoding=&quot;utf-8&quot;?>
[*]
[*]<wp:Notificationxmlns:wpwp:Notificationxmlns:wp=&quot;WPNotification&quot;>
[*]
[*]<wp:Tile>
[*]
[*]<wp:BackgroundImage>
[*]
[*]<backgroundimagepath>
[*]
[*]</wp:BackgroundImage>
[*]
[*]<wp:Count>
[*]
[*]<count>
[*]
[*]</wp:Count>
[*]
[*]<wp:Title>
[*]
[*]<title>
[*]
[*]</wp:Title>
[*]
[*]</wp:Tile>
[*]
[*]</wp:Notification>
[*]
2.Tokennotification:
request.Headers[&quot;X-WindowsPhone-Target&quot;]=&quot;token&quot;;
request.Headers
Message:
[*]&ldquo;Content-Type:text/xml\r\nX-WindowsPhone-Target:toast\r\n\r\n&rdquo;
[*]
[*]<?xmlversionxmlversion=&quot;1.0&quot;encoding=&quot;utf-8&quot;?>
[*]
[*]<wp:Notificationxmlns:wpwp:Notificationxmlns:wp=&quot;WPNotification&quot;>
[*]
[*]<wp:Toast>
[*]
[*]<wp:Text1>
[*]
[*]<string>
[*]
[*]</wp:Text1>
[*]
[*]<wp:Text2>
[*]
[*]<string>
[*]
[*]</wp:Text2>
[*]
[*]</wp:Toast>
[*]
[*]</wp:Notification>
[*]
3.rawnotification
request.Headers
request.BeginGetRequestStream();
StreamrequestStream=request.EndGetRequestStream();
requestStream.BeginWrite(message);
Response数据
response.StatusCode//Ok表示成功,否则可以查下面相应的错误码表,同时也可以查表得到当前状态
response.Headers
response.Headers
response.Headers
response.Headers[X-NotificationStatus
http://images.51cto.com/files/uploadimg/20100721/1539041.jpg
http://images.51cto.com/files/uploadimg/20100721/1539042.jpg
http://images.51cto.com/files/uploadimg/20100721/1539043.jpg
四.Windows Phone开发中创建客户端
HttpNotificationChannelhttpChannel=HttpNotificationChannel.Find(ChannelName);
httpChannel.Open();
//绑定notification
httpChannel.BindToShellToast();
httpChannel.BindToShellTile(uris);
//获取notificationchannelURI
httpChannel.ChannelUriUpdated+=newEventHandler<NotificationChannelUriEventArgs>(httpChannel_ChannelUriUpdated);
//获取Rawnotification
httpChannel.HttpNotificationReceived+=newEventHandler<HttpNotificationEventArgs>(httpChannel_HttpNotificationReceived);
//获取Toastnotification
httpChannel.ShellToastNotificationReceived+=newEventHandler<NotificationEventArgs>(httpChannel_ShellToastNotificationReceived);
//获取Pushnotificationerrormessage
httpChannel.ErrorOccurred+=newEventHandler<NotificationChannelErrorEventArgs>(httpChannel_ExceptionOccurred);
对于Tilenotification是由系统来接收的,所以这里没有相应的Event.
以上就是pushnotification的一些基本步骤,具体的实例在WP7TrainningKit里有。
<DIV align=right>【责任编辑:程华权 TEL:(010)68476606】
页:
[1]