一.简介

Push Notification windows phone 7中的特色功能之一,这个功能可以变相的让普通开发者实现多任务(尽管并不是真正的多任务)。它为手机端应用和webservice之间建立了一条专用的、持久的、稳定的通道来推送通知。当通道建立后,手机端应用可以接收webservice的任何信息。

二.分类

对于Push Notification主要有三种:

1.       Tile Notification:

是可以改变Quick Lanuch area内的图标内容(图片,文字等)的方式。不过这个需要把程序pin to start,才可以使用。

2.       Toast Notification:

是在屏幕上面可以显示一个提示栏的方式。当点击提示栏可以打开应用程序。

3.       Raw Notification:

是直接使用Http方式来接收(http polling)通知的方式。并且是不可见的,以后台方式传送通知。

对于以上几种通知,都需要一个服务端以push notification方式来发送通知, 也就是说要使用push notification都需要一个服务端。

三.创建服务器端

对于服务器端来说,发送不同的通知,都是以Http方式发出去的,但是在发送时,需要配置相应的参数,来告诉Push Notification Service所发送的类型是什么。

HttpWebRequest request = (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.Toast notification:

         request.Headers["X-WindowsPhone-Target"] = "toast";

request.Headers[X-NotificationClass]

 

Message :

"Content-Type: text/xml\r\nX-WindowsPhone-Target: token\r\n\r\n"

<?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>

 

      2.Token notification:

         request.Headers["X-WindowsPhone-Target"] = "token";

request.Headers[X-NotificationClass]

 

Message:

“Content-Type: text/xml\r\nX-WindowsPhone-Target: toast\r\n\r\n”

<?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>

3.raw notification

request.Headers[X-NotificationClass]

 

request.BeginGetRequestStream();

Stream requestStream = 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]

 

 

 

四.创建客户端

HttpNotificationChannel  httpChannel = HttpNotificationChannel.Find(ChannelName);

httpChannel.Open();

//绑定notification

httpChannel.BindToShellToast();

httpChannel.BindToShellTile(uris);

 

//获取notification channel URI

httpChannel.ChannelUriUpdated += new EventHandler<NotificationChannelUriEventArgs>(httpChannel_ChannelUriUpdated);

//获取Raw notification

httpChannel.HttpNotificationReceived += new EventHandler<HttpNotificationEventArgs>(httpChannel_HttpNotificationReceived);

          // 获取Toast notification

 httpChannel.ShellToastNotificationReceived += new EventHandler<NotificationEventArgs>(httpChannel_ShellToastNotificationReceived);

      // 获取Push notification error message

httpChannel.ErrorOccurred += new EventHandler<NotificationChannelErrorEventArgs>(httpChannel_ExceptionOccurred);

对于Tile notification是由系统来接收的,所以这里没有相应的Event.

以上就是push notification的一些基本步骤,具体的实例在WP7TrainningKit里有。

posted on 2010-07-19 17:23  小镇  阅读(3446)  评论(5编辑  收藏  举报