Windows Phone 7Li Jingnan / Wang Tao2011-7-151
2 days2
aboutanytao| Ethos<ethos:Member   id = “Wang Tao”   msn = anytao@live.comweibo = http://weibo.com/anytaorunat = “Senior System Architect”/>Jason | Ethos<ethos:Member   id = “Li Jingnan”   msn = zengnami@hotmail.comweibo= http://weibo.com/jn1981runat = “SE”/>
abouthttp://book.anytao.net
08 Push NotificationWang Tao / 2011-07-15
Session Outlineoverviewtitlestoastrawnotification service
3 kinds of notificationsRawnotification message content is app-specificdelivered directly to app only if it’s runningToastspecific xml schemaContent delivered to app if it’s runningIf app is not running, system displays Toast popup using notification message contentTilespecific xml schemaNever delivered to appIf user has pinned to app tile, system updates it using notification message content7
RawRawupdates it using notification message content8
Toast9
Tile10
What  is  Push ?
Push  VS  PullServer-PushClient-Pull
push notification(PN)WP7提供的一种允许服务器主动向WP7客户端直接发送通知的机制服务器端主动发起发送的是“通知”避免了Client-Pull通信模式的中多次轮询更省电更省网络流量给用户制造一种“多任务”的感觉便于创建高互动性的WP7网络应用程序(如IM)13
3 notificationsTile Notification效果:更新Tile(瓷片)显示格式:特定格式XML片段无论应用程序当前是否运行都接收Toast Notification效果:弹出Toast提示,用户可点击以启动应用程序格式:特定格式XML片段只有当应用程序未运行时才接收RAW Notification效果:由应用程序控制格式:自由格式二进制序列只有当应用程序正在运行时才接收14
3 notifications1415New photos online!Seattle, WA: Sunny and 85 degrees15
contentSub-TitleTitle16
PN & battery低电量状态只发送RAW通知MPNS将根据设备电量状态决定是否将通知发送到设备正常电量状态发送所有通知极低电量状态不发送任何通知17
PNbasicClient:Windows Phone DeviceMicrosoft Push Notification Service (MPNS) Provider:Web Application /Cloud Service18
PN processPush clientTileToastOpen ChanelAppOpen push channelReturn URIData to toastData to tileData to AppReturn Chanel URIMPNSYour serviceSend URI to serverEventSend push data to client19Send push data to URI
PN program model建立服务端Web Service。功能:a)接收客户端Chanle  URI并保存在列表中b)向MPN S发送通知建立WP7客户端端应用程序。功能:a)向MPNS注册Chanelb)将Chanel URI提交给服务端c)接收通知并处理、显示20
send notification// <Notification Channel URI>在Chanel创建时由MPNS生成,是Chanel的唯一标识stringsubscriptionUri = "<Notification Channel URI>";HttpWebRequestsendNotificationRequest = (HttpWebRequest)WebRequest.Create(subscriptionUri); //必须使用POST方法发送通知sendNotificationRequest.Method= "POST"; // 添加HTTP头X-MessageID作为消息标识(可选)sendNotificationRequest.Headers.Add("X-MessageID", "<UUID>"); sendNotificationRequest.ContentLength= notificationMessage.Length; // 设置要发送的通知内容<payload>byte[] notificationMessage = new byte[] {<payload>}; using(Stream requestStream = sendNotificationRequest.GetRequestStream()) { requestStream.Write(notificationMessage, 0, notificationMessage.Length); } //向MPNS发送通知并获取响应HttpWebResponseresponse = (HttpWebResponse)sendNotificationRequest.GetResponse(); //从响应的HTTP头中提取相关结果stringnotificationStatus = response.Headers["X-NotificationStatus"]; stringnotificationChannelStatus = response.Headers["X-SubscriptionStatus"]; stringdeviceConnectionStatus = response.Headers["X-DeviceConnectionStatus"];21
tile notificationsendNotificationRequest.ContentType = "text/xml";sendNotificationRequest.Headers.Add("X-WindowsPhone-Target", "token");sendNotificationRequest.Headers.Add("X-NotificationClass", “1"); 22tilenotification HTTPheadertilenotification contentstring tileMessage = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" +"<wp:Notificationxmlns:wp=\"WPNotification\">" +   "<wp:Tile>" +      "<wp:BackgroundImage><background image path></wp:BackgroundImage>" +      "<wp:Count><count></wp:Count>" +      "<wp:Title><title></wp:Title>" +   "</wp:Tile> " +"</wp:Notification>";
toastsendNotificationRequest.ContentType = "text/xml";sendNotificationRequest.Headers.Add("X-WindowsPhone-Target", "toast");sendNotificationRequest.Headers.Add("X-NotificationClass", “1");23Toast http headerToast contentstring toastMessage = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" +"<wp:Notificationxmlns:wp=\"WPNotification\">" +   "<wp:Toast>" +      "<wp:Text1><string></wp:Text1>" +      "<wp:Text2><string></wp:Text2>" +   "</wp:Toast>" +"</wp:Notification>";
send notification// <Notification Channel URI>在Chanel创建时由MPNS生成,是Chanel的唯一标识stringsubscriptionUri = "<Notification Channel URI>";HttpWebRequestsendNotificationRequest = (HttpWebRequest)WebRequest.Create(subscriptionUri); //必须使用POST方法发送通知sendNotificationRequest.Method= "POST"; // 添加HTTP头X-MessageID作为消息标识(可选)sendNotificationRequest.Headers.Add("X-MessageID", "<UUID>"); sendNotificationRequest.ContentLength= notificationMessage.Length; // 设置要发送的通知内容<payload>byte[] notificationMessage = new byte[] {<payload>}; using(Stream requestStream = sendNotificationRequest.GetRequestStream()) { requestStream.Write(notificationMessage, 0, notificationMessage.Length); } //向MPNS发送通知并获取响应HttpWebResponseresponse = (HttpWebResponse)sendNotificationRequest.GetResponse(); //从响应的HTTP头中提取相关结果stringnotificationStatus = response.Headers["X-NotificationStatus"]; stringnotificationChannelStatus = response.Headers["X-SubscriptionStatus"]; stringdeviceConnectionStatus = response.Headers["X-DeviceConnectionStatus"];24
raw  notificaitionsendNotificationRequest.Headers.Add("X-NotificationClass", “1");25RAW http headerRAWhttp contentnew byte[] {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08};
demo26/ title/ toast/ raw/ notification service04 notification
thank you27thank youwww.anytao.com
28

08 wp7 push notification