Download - 08 wp7 push notification
Windows Phone 7
Li Jingnan / Wang Tao2011-7-15
1
Windows Phone Microsoft Corporation.
2 days 9:00 Introduction Anytao
10:30 Build a Silverlight Application Jason
13:00 Application Bar Anytao
14:30 Panorama and Pivots Jason
16:00 Launcher and Chooser Jason
9:00 Isolation Storage Anytao
10:30 Application Lifecycle Jason
13:00 Push Notification Anytao
14:30 Multitasking Jason
16:00 Local Database Anytao
9:00 Design Apps Using Expression Blend and Metro Jason
10:30 Marketing your Windows Phone Application Jason
13:00 Working with Azure Anytao
2
Windows Phone Microsoft Corporation.
about
anytao | Ethos
<ethos:Member id = “Wang Tao” msn = [email protected] weibo = http://weibo.com/anytao runat = “Senior System Architect”/>
Jason | Ethos
<ethos:Member id = “Li Jingnan” msn = [email protected] weibo = http://weibo.com/jn1981 runat = “SE”/>
Windows Phone Microsoft Corporation.
abouthttp://book.anytao.net
08 Push Notification
Wang Tao / 2011-07-15
Windows Phone Microsoft Corporation.
Session Outline overview titles toast raw notification service
Windows Phone Microsoft Corporation.7
3 kinds of notifications
Raw notification message content is app-specific delivered directly to app only if it’s running
Toast specific xml schema Content delivered to app if it’s running If app is not running, system displays Toast popup using notification message
content Tile
specific xml schema Never delivered to app If user has pinned to app tile, system updates it using notification message content
Windows Phone Microsoft Corporation.8
Raw
Raw updates it using notification message
content
Windows Phone Microsoft Corporation.9
Toast
Windows Phone Microsoft Corporation.10
Tile
Windows Phone Microsoft Corporation.
What is Push ?
Windows Phone Microsoft Corporation.
Push VS Pull
Client-PullServer-Push
Windows Phone Microsoft Corporation.
push notification(PN)
WP7 提供的一种允许服务器主动向 WP7 客户端直接发送通知的机制 服务器端主动发起 发送的是“通知”
避免了 Client-Pull 通信模式的中多次轮询 更省电 更省网络流量
给用户制造一种“多任务”的感觉 便于创建高互动性的 WP7 网络应用程序(如 IM )
13
Windows Phone Microsoft Corporation.
3 notifications Tile Notification
效果:更新 Tile( 瓷片 ) 显示 格式:特定格式 XML 片段 无论应用程序当前是否运行都接收
Toast Notification 效果:弹出 Toast 提示,用户可点击以启动应
用程序 格式:特定格式 XML 片段 只有当应用程序未运行时才接收
RAW Notification 效果:由应用程序控制 格式:自由格式二进制序列 只有当应用程序正在运行时才接收1
4
Windows Phone Microsoft Corporation.15
New photos online!
Seattle, WA: Sunny and 85 degrees
1415
3 notifications
Windows Phone Microsoft Corporation.16
Title
Sub-Title
content
Windows Phone Microsoft Corporation.
PN & battery
17
极低电量状态不发送任何通知
低电量状态只发送 RAW 通
知
正常电量状态发送所有通知
MPNS 将根据设备电量状态决定是否将通知发送到设备
Windows Phone Microsoft Corporation.
PN basic
18
Client:Windows Phone Device
Microsoft Push Notification Service (MPNS)
Provider:Web Application / Cloud Service
Windows Phone Microsoft Corporation.
PN process
19
Push client
MPNSYour
service
Tile
App
Toast
Send push data to URI
Send push data to
client
Send URI to server
Open push channelReturn URIData to App
Data to toast
Data to tile
Event
Open Chanel
Return Chanel URI
Windows Phone Microsoft Corporation.
PN program model
20
建立服务端Web Service。功能:a) 接收客户端 Chanle URI 并保存在列表中b) 向 MPN S发送通知
建立WP7客户端端应用程序。功能:a) 向 MPNS注册 Chanelb) 将 Chanel URI 提交给服务端c) 接收通知并处理、显示
Windows Phone Microsoft Corporation.
send notification// <Notification Channel URI> 在 Chanel 创建时由 MPNS 生成,是 Chanel 的唯一标识string subscriptionUri = "<Notification Channel URI>";
HttpWebRequest sendNotificationRequest = (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 发送通知并获取响应HttpWebResponse response = (HttpWebResponse)sendNotificationRequest.GetResponse();
// 从响应的 HTTP 头中提取相关结果string notificationStatus = response.Headers["X-NotificationStatus"];
string notificationChannelStatus = response.Headers["X-SubscriptionStatus"];
string deviceConnectionStatus = response.Headers["X-DeviceConnectionStatus"];21
Windows Phone Microsoft Corporation.
tile notification
sendNotificationRequest.ContentType = "text/xml";
sendNotificationRequest.Headers.Add("X-WindowsPhone-Target", "token");
sendNotificationRequest.Headers.Add("X-NotificationClass", “1");
22
tile notification HTTP header
string tileMessage = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" +"<wp:Notification xmlns: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>";
tile notification content
Windows Phone Microsoft Corporation.
toast
sendNotificationRequest.ContentType = "text/xml";
sendNotificationRequest.Headers.Add("X-WindowsPhone-Target", "toast");
sendNotificationRequest.Headers.Add("X-NotificationClass", “1");
23
Toast http header
string toastMessage = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" +"<wp:Notification xmlns:wp=\"WPNotification\">" + "<wp:Toast>" + "<wp:Text1><string></wp:Text1>" + "<wp:Text2><string></wp:Text2>" + "</wp:Toast>" +"</wp:Notification>";
Toast content
Windows Phone Microsoft Corporation.
send notification// <Notification Channel URI> 在 Chanel 创建时由 MPNS 生成,是 Chanel 的唯一标识string subscriptionUri = "<Notification Channel URI>";
HttpWebRequest sendNotificationRequest = (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 发送通知并获取响应HttpWebResponse response = (HttpWebResponse)sendNotificationRequest.GetResponse();
// 从响应的 HTTP 头中提取相关结果string notificationStatus = response.Headers["X-NotificationStatus"];
string notificationChannelStatus = response.Headers["X-SubscriptionStatus"];
string deviceConnectionStatus = response.Headers["X-DeviceConnectionStatus"];24
Windows Phone Microsoft Corporation.
raw notificaition
sendNotificationRequest.Headers.Add("X-NotificationClass", “1");
25
RAW http header
new byte[] {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08};
RAW http content
Windows Phone Microsoft Corporation.26
demo04 notification
/ title/ toast/ raw/ notification service
Windows Phone Microsoft Corporation.27
Click to add picturethank youthank youwww.anytao.com
Windows Phone Microsoft Corporation.
© 2011 Microsoft Corporation.
All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries.
The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation.
MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.
28