Windows Phone開發學習指南
本文和大家一起學習一下Windows Phone開發的概念,PushNotification是windowsphone7中的特色功能之一,它為手機端應用和webservice之間建立了一條專用的、持久的、穩定的通道來推送通知。當通道建立后,手機端應用可以接收webservice的任何信息。
一起學Windows Phone開發
一.簡介
PushNotification是windowsphone7中的特色功能之一,這個功能可以變相的讓普通開發者實現多任務(盡管并不是真正的多任務)。它為手機端應用和webservice之間建立了一條專用的、持久的、穩定的通道來推送通知。當通道建立后,手機端應用可以接收webservice的任何信息。
二.分類
對于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[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數據
response.StatusCode//Ok表示成功,否則可以查下面相應的錯誤碼表,同時也可以查表得到當前狀態
response.Headers[X-MessageID]
response.Headers[X-DeviceConnectionStatus]
response.Headers[X-SubscriptionStatus]
response.Headers[X-NotificationStatus
四.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里有。