Windows Phone開發(fā)學(xué)習(xí)指南
本文和大家一起學(xué)習(xí)一下Windows Phone開發(fā)的概念,PushNotification是windowsphone7中的特色功能之一,它為手機(jī)端應(yīng)用和webservice之間建立了一條專用的、持久的、穩(wěn)定的通道來(lái)推送通知。當(dāng)通道建立后,手機(jī)端應(yīng)用可以接收webservice的任何信息。
一起學(xué)Windows Phone開發(fā)
一.簡(jiǎn)介
PushNotification是windowsphone7中的特色功能之一,這個(gè)功能可以變相的讓普通開發(fā)者實(shí)現(xiàn)多任務(wù)(盡管并不是真正的多任務(wù))。它為手機(jī)端應(yīng)用和webservice之間建立了一條專用的、持久的、穩(wěn)定的通道來(lái)推送通知。當(dāng)通道建立后,手機(jī)端應(yīng)用可以接收webservice的任何信息。
二.分類
對(duì)于PushNotification主要有三種:
1.TileNotification:
是可以改變QuickLanucharea內(nèi)的圖標(biāo)內(nèi)容(圖片,文字等)的方式。不過(guò)這個(gè)需要把程序pintostart,才可以使用。
2.ToastNotification:
是在屏幕上面可以顯示一個(gè)提示欄的方式。當(dāng)點(diǎn)擊提示欄可以打開應(yīng)用程序。
3.RawNotification:
是直接使用Http方式來(lái)接收(httppolling)通知的方式。并且是不可見的,以后臺(tái)方式傳送通知。
對(duì)于以上幾種通知,都需要一個(gè)服務(wù)端以pushnotification方式來(lái)發(fā)送通知,也就是說(shuō)要使用pushnotification都需要一個(gè)服務(wù)端。
三.Windows Phone開發(fā)中創(chuàng)建服務(wù)器端
對(duì)于服務(wù)器端來(lái)說(shuō),發(fā)送不同的通知,都是以Http方式發(fā)出去的,但是在發(fā)送時(shí),需要配置相應(yīng)的參數(shù),來(lái)告訴PushNotificationService所發(fā)送的類型是什么。
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[X-NotificationClass]
Message:
- "Content-Type:text/xml\r\nX-WindowsPhone-Target:token\r\n\r\n"
- <?xmlversionxmlversion="1.0"encoding="utf-8"?>
- <wp:Notificationxmlns:wpwp:Notificationxmlns:wp="WPNotification">
- <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["X-WindowsPhone-Target"]="token";
request.Headers[X-NotificationClass]
Message:
- “Content-Type:text/xml\r\nX-WindowsPhone-Target:toast\r\n\r\n”
- <?xmlversionxmlversion="1.0"encoding="utf-8"?>
- <wp:Notificationxmlns:wpwp:Notificationxmlns:wp="WPNotification">
- <wp:Toast>
- <wp:Text1>
- <string>
- </wp:Text1>
- <wp:Text2>
- <string>
- </wp:Text2>
- </wp:Toast>
- </wp:Notification>
3.rawnotification
request.Headers[X-NotificationClass]
request.BeginGetRequestStream();
StreamrequestStream=request.EndGetRequestStream();
requestStream.BeginWrite(message);
Response數(shù)據(jù)
response.StatusCode//Ok表示成功,否則可以查下面相應(yīng)的錯(cuò)誤碼表,同時(shí)也可以查表得到當(dāng)前狀態(tài)
response.Headers[X-MessageID]
response.Headers[X-DeviceConnectionStatus]
response.Headers[X-SubscriptionStatus]
response.Headers[X-NotificationStatus
四.Windows Phone開發(fā)中創(chuàng)建客戶端
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);
對(duì)于Tilenotification是由系統(tǒng)來(lái)接收的,所以這里沒有相應(yīng)的Event.
以上就是pushnotification的一些基本步驟,具體的實(shí)例在WP7TrainningKit里有。