Windows Phone7开发之 Push+Notification

webphp 13年前
     <p style="text-indent:-21pt;margin:0cm 0cm 0pt 21pt;"><span style="line-height:19px;">一.</span><span style="line-height:19px;font-family:宋体;">简介</span></p>    <p style="text-indent:0px;margin:0cm 0cm 0pt 21pt;">Push Notification <span style="line-height:19px;font-family:宋体;">是</span>windows phone 7<span style="line-height:19px;font-family:宋体;">中的特色功能之一,这个功能可以变相的让普通开发者实现多任务(尽管并不是真正的多任务)。它为手机端应用和</span>webservice<span style="line-height:19px;font-family:宋体;">之间建立了一条专用的、持久的、稳定的通道来推送通知。当通道建立后,手机端应用可以接收</span>webservice<span style="line-height:19px;font-family:宋体;">的任何信息。</span></p>    <p style="text-indent:0px;margin:0cm 0cm 0pt 21pt;"></p>    <p style="text-indent:-21pt;margin:0cm 0cm 0pt 21pt;"><span style="line-height:19px;">二.</span><span style="line-height:19px;font-family:宋体;">分类</span></p>    <p style="text-indent:0px;margin:0cm 0cm 0pt 21pt;"><span style="line-height:19px;font-family:宋体;">对于</span>Push Notification<span style="line-height:19px;font-family:宋体;">主要有三种:</span></p>    <p style="text-indent:-18pt;margin:0cm 0cm 0pt 39pt;"><span style="line-height:19px;">1.<span style="font:7pt 'Times New Roman';">       </span></span>Tile Notification:</p>    <p style="text-indent:0px;margin:0cm 0cm 0pt 39pt;"><span style="line-height:19px;font-family:宋体;">是可以改变</span>Quick Lanuch area<span style="line-height:19px;font-family:宋体;">内的图标内容(图片,文字等)的方式。不过这个需要把程序</span>pin to start,<span style="line-height:19px;font-family:宋体;">才可以使用。</span></p>    <p style="text-indent:-18pt;margin:0cm 0cm 0pt 39pt;"><span style="line-height:19px;">2.<span style="font:7pt 'Times New Roman';">       </span></span>Toast Notification:</p>    <p style="text-indent:0px;margin:0cm 0cm 0pt 39pt;"><span style="line-height:19px;font-family:宋体;">是在屏幕上面可以显示一个提示栏的方式。当点击提示栏可以打开应用程序。</span></p>    <p style="text-indent:-18pt;margin:0cm 0cm 0pt 39pt;"><span style="line-height:19px;">3.<span style="font:7pt 'Times New Roman';">       </span></span>Raw Notification:</p>    <p style="text-indent:0px;margin:0cm 0cm 0pt 39pt;"><span style="line-height:19px;font-family:宋体;">是直接使用</span>Http<span style="line-height:19px;font-family:宋体;">方式来接收(</span>http polling<span style="line-height:19px;font-family:宋体;">)通知的方式。并且是不可见的,以后台方式传送通知。</span></p>    <p style="text-indent:0px;margin:0cm 0cm 0pt 39pt;"><span style="line-height:19px;font-family:宋体;">对于以上几种通知,都需要一个服务端以</span>push notification<span style="line-height:19px;font-family:宋体;">方式来发送通知,</span> <span style="line-height:19px;font-family:宋体;">也就是说要使用</span>push notification<span style="line-height:19px;font-family:宋体;">都需要一个服务端。</span></p>    <p style="text-indent:-21pt;margin:0cm 0cm 0pt 21pt;"><span style="line-height:19px;">三.</span><span style="line-height:19px;font-family:宋体;">创建服务器端</span></p>    <p style="text-indent:0px;margin:0cm 0cm 0pt 21pt;"><span style="line-height:19px;font-family:宋体;">对于服务器端来说,发送不同的通知,都是以</span>Http<span style="line-height:19px;font-family:宋体;">方式发出去的,但是在发送时,需要配置相应的参数,来告诉</span>Push Notification Service<span style="line-height:19px;font-family:宋体;">所发送的类型是什么。</span></p>    <p style="text-align:left;text-indent:21pt;margin:5px auto;" align="left"><span style="line-height:19px;font-family:NSimSun;color:#2b91af;font-size:9.5pt;">HttpWebRequest</span><span style="line-height:19px;font-family:NSimSun;font-size:9.5pt;"> request = (<span style="line-height:19px;color:#2b91af;">HttpWebRequest</span>)<span style="line-height:19px;color:#2b91af;">WebRequest</span>.Create(channelUri);</span></p>    <p style="text-indent:0px;margin:5px auto;" align="left"><span style="line-height:19px;font-family:NSimSun;font-size:9.5pt;">     request.Method = <span style="line-height:19px;color:#2b91af;">WebRequestMethods</span>.<span style="line-height:19px;color:#2b91af;">Http</span>.Post;</span></p>    <p style="text-indent:0px;margin:5px auto;" align="left"><span style="line-height:19px;font-family:NSimSun;font-size:9.5pt;">     request.ContentType = <span style="line-height:19px;color:#a31515;">"text/xml; charset=utf-8"</span>;</span></p>    <p style="text-indent:0px;margin:5px auto;" align="left"><span style="line-height:19px;font-family:NSimSun;font-size:9.5pt;">     request.ContentLength = notificationmessage.Length;</span></p>    <p style="text-indent:0px;margin:5px auto;" align="left"><span style="line-height:19px;font-family:NSimSun;font-size:9.5pt;">     request.Headers[<span style="line-height:19px;color:#a31515;">"X-MessageID"</span>] = <span style="line-height:19px;color:#2b91af;">Guid</span>.NewGuid().ToString();</span></p>    <p style="text-indent:0px;margin:5px auto;" align="left"><span style="line-height:19px;font-family:NSimSun;font-size:9.5pt;">     1.Toast notification:</span></p>    <p style="text-indent:0px;margin:5px auto;" align="left"><span style="line-height:19px;font-family:NSimSun;font-size:9.5pt;">         request.Headers[<span style="line-height:19px;color:#a31515;">"X-WindowsPhone-Target"</span>] = <span style="line-height:19px;color:#a31515;">"toast"</span>;</span></p>    <p style="text-align:left;text-indent:0px;margin:0cm 0cm 0pt 39pt;" align="left"><span style="line-height:19px;font-family:NSimSun;font-size:9.5pt;">request.Headers[<span style="line-height:19px;color:#a31515;">X-NotificationClass</span><span style="line-height:19px;color:black;">]</span></span></p>    <p style="text-indent:0px;margin:5px auto;" align="left"> </p>    <p style="text-align:left;text-indent:0px;margin:5px auto;word-break:break-all;" align="left"><span style="line-height:19px;font-family:Consolas;color:black;">Message :</span></p>    <p style="text-align:left;text-indent:0px;margin:5px auto;word-break:break-all;" align="left"><span style="line-height:19px;font-family:Consolas;color:#a31515;">"Content-Type: text/xml\r\nX-WindowsPhone-Target: token\r\n\r\n"</span></p>    <p style="text-align:left;text-indent:0px;margin:5px auto;word-break:break-all;" align="left"><span style="line-height:19px;font-family:Consolas;color:#a31515;"><?xml version="1.0" encoding="utf-8"?></span></p>    <p style="text-align:left;text-indent:10.5pt;margin:5px auto;word-break:break-all;" align="left"><span style="line-height:19px;font-family:Consolas;color:#a31515;"><wp:Notification xmlns:wp="WPNotification"></span></p>    <p style="text-align:left;text-indent:0px;margin:5px auto;word-break:break-all;" align="left"><span style="line-height:19px;font-family:Consolas;color:black;">   </span><span style="line-height:19px;font-family:Consolas;color:#a31515;">  <wp:Tile></span></p>    <p style="text-align:left;text-indent:0px;margin:5px auto;word-break:break-all;" align="left"><span style="line-height:19px;font-family:Consolas;color:black;">      </span><span style="line-height:19px;font-family:Consolas;color:#a31515;"> <wp:BackgroundImage></span></p>    <p style="text-align:left;text-indent:0px;margin:5px auto;word-break:break-all;" align="left"><span style="line-height:19px;font-family:Consolas;color:#a31515;">            <background image path></span></p>    <p style="text-align:left;text-indent:0px;margin:5px auto;word-break:break-all;" align="left"><span style="line-height:19px;font-family:Consolas;color:#a31515;">        </wp:BackgroundImage></span></p>    <p style="text-align:left;text-indent:0px;margin:5px auto;word-break:break-all;" align="left"><span style="line-height:19px;font-family:Consolas;color:black;">      </span><span style="line-height:19px;font-family:Consolas;color:#a31515;"> <wp:Count></span></p>    <p style="text-align:left;text-indent:0px;margin:5px auto;word-break:break-all;" align="left"><span style="line-height:19px;font-family:Consolas;color:#a31515;">             <count></span></p>    <p style="text-align:left;text-indent:0px;margin:5px auto;word-break:break-all;" align="left"><span style="line-height:19px;font-family:Consolas;color:#a31515;">        </wp:Count></span></p>    <p style="text-align:left;text-indent:0px;margin:5px auto;word-break:break-all;" align="left"><span style="line-height:19px;font-family:Consolas;color:black;">      </span><span style="line-height:19px;font-family:Consolas;color:#a31515;"> <wp:Title></span></p>    <p style="text-align:left;text-indent:0px;margin:5px auto;word-break:break-all;" align="left"><span style="line-height:19px;font-family:Consolas;color:#a31515;">          <title></span></p>    <p style="text-align:left;text-indent:0px;margin:5px auto;word-break:break-all;" align="left"><span style="line-height:19px;font-family:Consolas;color:#a31515;">       </wp:Title></span></p>    <p style="text-align:left;text-indent:0px;margin:5px auto;word-break:break-all;" align="left"><span style="line-height:19px;font-family:Consolas;color:black;">   </span><span style="line-height:19px;font-family:Consolas;color:#a31515;"> </wp:Tile></span></p>    <p style="text-indent:0px;margin:5px auto;" align="left"><span style="line-height:19px;font-family:Consolas;color:#a31515;"></wp:Notification></span></p>    <p style="text-indent:0px;margin:5px auto;" align="left"><span style="line-height:19px;font-family:NSimSun;font-size:9.5pt;">      2.Token notification:</span></p>    <p style="text-indent:0px;margin:5px auto;" align="left"><span style="line-height:19px;font-family:NSimSun;font-size:9.5pt;">         request.Headers[<span style="line-height:19px;color:#a31515;">"X-WindowsPhone-Target"</span>] = <span style="line-height:19px;color:#a31515;">"token"</span>;</span></p>    <p style="text-align:left;text-indent:0px;margin:0cm 0cm 0pt 39pt;" align="left"><span style="line-height:19px;font-family:NSimSun;font-size:9.5pt;">request.Headers[<span style="line-height:19px;color:#a31515;">X-NotificationClass</span><span style="line-height:19px;color:black;">]</span></span></p>    <p style="text-indent:0px;margin:5px auto;" align="left"> </p>    <p style="text-align:left;text-indent:0px;margin:5px auto;word-break:break-all;" align="left"><span style="line-height:19px;font-family:Consolas;color:black;">Message:</span></p>    <p style="text-align:left;text-indent:0px;margin:5px auto;word-break:break-all;" align="left"><span style="line-height:19px;font-family:Consolas;color:#a31515;">“Content-Type: text/xml\r\nX-WindowsPhone-Target: toast\r\n\r\n”</span></p>    <p style="text-align:left;text-indent:0px;margin:5px auto;word-break:break-all;" align="left"><span style="line-height:19px;font-family:Consolas;color:#a31515;"><?xml version="1.0" encoding="utf-8"?></span></p>    <p style="text-align:left;text-indent:10.5pt;margin:5px auto;word-break:break-all;" align="left"><span style="line-height:19px;font-family:Consolas;color:#a31515;"><wp:Notification xmlns:wp="WPNotification"></span></p>    <p style="text-align:left;text-indent:0px;margin:5px auto;word-break:break-all;" align="left"><span style="line-height:19px;font-family:Consolas;color:#a31515;">     <wp:Toast></span></p>    <p style="text-align:left;text-indent:0px;margin:5px auto;word-break:break-all;" align="left"><span style="line-height:19px;font-family:Consolas;color:#a31515;">        <wp:Text1></span></p>    <p style="text-align:left;text-indent:0px;margin:5px auto;word-break:break-all;" align="left"><span style="line-height:19px;font-family:Consolas;color:#a31515;">               <string></span></p>    <p style="text-align:left;text-indent:0px;margin:5px auto;word-break:break-all;" align="left"><span style="line-height:19px;font-family:Consolas;color:#a31515;">        </wp:Text1></span></p>    <p style="text-align:left;text-indent:0px;margin:5px auto;word-break:break-all;" align="left"><span style="line-height:19px;font-family:Consolas;color:#a31515;">        <wp:Text2></span></p>    <p style="text-align:left;text-indent:0px;margin:5px auto;word-break:break-all;" align="left"><span style="line-height:19px;font-family:Consolas;color:#a31515;">        <string></span></p>    <p style="text-align:left;text-indent:0px;margin:5px auto;word-break:break-all;" align="left"><span style="line-height:19px;font-family:Consolas;color:#a31515;">        </wp:Text2></span></p>    <p style="text-align:left;text-indent:0px;margin:5px auto;word-break:break-all;" align="left"><span style="line-height:19px;font-family:Consolas;color:#a31515;">    </wp:Toast></span></p>    <p style="text-align:left;text-indent:0px;margin:5px auto;word-break:break-all;" align="left"><span style="line-height:19px;font-family:Consolas;color:#a31515;"></wp:Notification></span></p>    <p style="text-indent:0px;margin:0cm 0cm 0pt 21pt;">3.raw notification</p>    <p style="text-align:left;text-indent:0px;margin:0cm 0cm 0pt 39pt;" align="left"><span style="line-height:19px;font-family:NSimSun;font-size:9.5pt;">request.Headers[<span style="line-height:19px;color:#a31515;">X-NotificationClass</span><span style="line-height:19px;color:black;">]</span></span></p>    <p style="text-indent:0px;margin:0cm 0cm 0pt 39pt;"> </p>    <p style="text-indent:0px;margin:5px auto;" align="left"><span style="line-height:19px;font-family:NSimSun;font-size:9.5pt;">request.BeginGetRequestStream();</span></p>    <p style="text-indent:0px;margin:5px auto;" align="left"><span style="line-height:19px;font-family:NSimSun;color:#2b91af;font-size:9.5pt;">Stream</span><span style="line-height:19px;font-family:NSimSun;font-size:9.5pt;"> requestStream = request.EndGetRequestStream();</span></p>    <p style="text-indent:0px;margin:5px auto;" align="left"><span style="line-height:19px;font-family:NSimSun;font-size:9.5pt;">requestStream.BeginWrite(message);</span></p>    <p style="text-indent:0px;margin:5px auto;" align="left"> </p>    <p style="text-indent:0px;margin:5px auto;" align="left"><span style="line-height:19px;font-family:NSimSun;font-size:9.5pt;">Response </span><span style="line-height:19px;font-family:宋体;font-size:9.5pt;">数据</span></p>    <p style="text-indent:0px;margin:5px auto;" align="left"><span style="line-height:19px;font-family:NSimSun;font-size:9.5pt;">response.StatusCode<span style="line-height:19px;color:green;">//Ok </span></span><span style="line-height:19px;font-family:宋体;color:green;font-size:9.5pt;">表示成功,否则可以查下面相应的错误码表,同时也可以查表得到当前状态</span></p>    <p style="text-indent:0px;margin:5px auto;" align="left"><span style="line-height:19px;font-family:NSimSun;font-size:9.5pt;">response.Headers[<span style="line-height:19px;color:#a31515;">X-MessageID</span>]</span></p>    <p style="text-indent:0px;margin:5px auto;" align="left"><span style="line-height:19px;font-family:NSimSun;font-size:9.5pt;">response.Headers[<span style="line-height:19px;color:#a31515;">X-DeviceConnectionStatus</span>]</span></p>    <p style="text-indent:0px;margin:5px auto;" align="left"><span style="line-height:19px;font-family:NSimSun;font-size:9.5pt;">response.Headers[<span style="line-height:19px;color:#a31515;">X-SubscriptionStatus</span>]</span></p>    <p style="text-indent:0px;margin:5px auto;" align="left"><span style="line-height:19px;font-family:NSimSun;font-size:9.5pt;">response.Headers[<span style="line-height:19px;color:#a31515;">X-NotificationStatus</span>]</span></p>    <p style="text-indent:0px;margin:5px auto;" align="left"> </p>    <p style="text-indent:-21pt;margin:0cm 0cm 0pt 21pt;"><span style="line-height:19px;">四.</span><span style="line-height:19px;font-family:宋体;">创建客户端</span></p>    <p style="text-align:left;text-indent:21pt;margin:5px auto;" align="left"><span style="line-height:19px;font-family:NSimSun;color:#2b91af;font-size:9.5pt;">HttpNotificationChannel</span><span style="line-height:19px;font-family:NSimSun;font-size:9.5pt;">  httpChannel = <span style="line-height:19px;color:#2b91af;">HttpNotificationChannel</span>.Find(ChannelName);</span></p>    <p style="text-align:left;text-indent:21pt;margin:5px auto;" align="left"><span style="line-height:19px;font-family:NSimSun;font-size:9.5pt;">httpChannel.Open();</span></p>    <p style="text-align:left;text-indent:0px;margin:0cm 0cm 0pt 21pt;" align="left"><span style="line-height:19px;font-family:NSimSun;color:green;font-size:9.5pt;">//</span><span style="line-height:19px;font-family:宋体;color:green;font-size:9.5pt;">绑定</span><span style="line-height:19px;font-family:NSimSun;color:green;font-size:9.5pt;">notification</span></p>    <p style="text-align:left;text-indent:0px;margin:0cm 0cm 0pt 21pt;" align="left"><span style="line-height:19px;font-family:NSimSun;font-size:9.5pt;">httpChannel.BindToShellToast();</span></p>    <p style="text-align:left;text-indent:0px;margin:0cm 0cm 0pt 21pt;" align="left"><span style="line-height:19px;font-family:NSimSun;font-size:9.5pt;">httpChannel.BindToShellTile(uris);</span></p>    <p style="text-align:left;text-indent:21pt;margin:5px auto;" align="left"> </p>    <p style="text-align:left;text-indent:21pt;margin:5px auto;" align="left"><span style="line-height:19px;font-family:NSimSun;color:green;font-size:9.5pt;">//</span><span style="line-height:19px;font-family:宋体;color:green;font-size:9.5pt;">获取</span><span style="line-height:19px;font-family:NSimSun;color:green;font-size:9.5pt;">notification channel URI</span></p>    <p style="text-align:left;text-indent:0px;margin:0cm 0cm 0pt 21pt;" align="left"><span style="line-height:19px;font-family:NSimSun;font-size:9.5pt;">httpChannel.ChannelUriUpdated += <span style="line-height:19px;color:blue;">new</span> <span style="line-height:19px;color:#2b91af;">EventHandler</span><<span style="line-height:19px;color:#2b91af;">NotificationChannelUriEventArgs</span>>(httpChannel_ChannelUriUpdated);</span></p>    <p style="text-align:left;text-indent:21pt;margin:5px auto;" align="left"><span style="line-height:19px;font-family:NSimSun;color:green;font-size:9.5pt;">//</span><span style="line-height:19px;font-family:宋体;color:green;font-size:9.5pt;">获取</span><span style="line-height:19px;font-family:NSimSun;color:green;font-size:9.5pt;">Raw notification</span></p>    <p style="text-align:left;text-indent:0px;margin:0cm 0cm 0pt 21pt;" align="left"><span style="line-height:19px;font-family:NSimSun;font-size:9.5pt;">httpChannel.HttpNotificationReceived += <span style="line-height:19px;color:blue;">new</span> <span style="line-height:19px;color:#2b91af;">EventHandler</span><<span style="line-height:19px;color:#2b91af;">HttpNotificationEventArgs</span>>(httpChannel_HttpNotificationReceived);</span></p>    <p style="text-indent:0px;margin:5px auto;" align="left"><span style="line-height:19px;font-family:NSimSun;font-size:9.5pt;">          <span style="line-height:19px;color:green;">// </span></span><span style="line-height:19px;font-family:宋体;color:green;font-size:9.5pt;">获取</span><span style="line-height:19px;font-family:NSimSun;color:green;font-size:9.5pt;">Toast notification</span></p>    <p style="text-align:left;text-indent:-4.75pt;margin:0cm 0cm 0pt 25.75pt;" align="left"><span style="line-height:19px;font-family:NSimSun;font-size:9.5pt;"> httpChannel.ShellToastNotificationReceived += <span style="line-height:19px;color:blue;">new</span> <span style="line-height:19px;color:#2b91af;">EventHandler</span><<span style="line-height:19px;color:#2b91af;">NotificationEventArgs</span>>(httpChannel_ShellToastNotificationReceived);</span></p>    <p style="text-indent:0px;margin:5px auto;" align="left"><span style="line-height:19px;font-family:NSimSun;font-size:9.5pt;">      <span style="line-height:19px;color:green;">// </span></span><span style="line-height:19px;font-family:宋体;color:green;font-size:9.5pt;">获取</span><span style="line-height:19px;font-family:NSimSun;color:green;font-size:9.5pt;">Push notification error message</span></p>    <p style="text-align:left;text-indent:0px;margin:0cm 0cm 0pt 21pt;" align="left"><span style="line-height:19px;font-family:NSimSun;font-size:9.5pt;">httpChannel.ErrorOccurred += <span style="line-height:19px;color:blue;">new</span> <span style="line-height:19px;color:#2b91af;">EventHandler</span><<span style="line-height:19px;color:#2b91af;">NotificationChannelErrorEventArgs</span>>(httpChannel_ExceptionOccurred);</span></p>    <p style="text-indent:0px;margin:0cm 0cm 0pt 21pt;"><span style="line-height:19px;font-family:宋体;">对于</span>Tile notification<span style="line-height:19px;font-family:宋体;">是由系统来接收的,所以这里没有相应的</span>Event.</p>    <p style="text-indent:0px;margin:0cm 0cm 0pt 21pt;"><span style="line-height:19px;font-family:宋体;">以上就是</span>push notification<span style="line-height:19px;font-family:宋体;">的一些基本步骤,具体的实例在</span>WP7TrainningKit<span style="line-height:19px;font-family:宋体;">里有。</span></p>