天天看點

一起學Windows Phone 7開發(十二.Push Notification)

一.簡介

Push Notification 是windows phone 7中的特色功能之一,這個功能可以變相的讓普通開發者實作多任務(盡管并不是真正的多任務)。它為手機端應用和webservice之間建立了一條專用的、持久的、穩定的通道來推送通知。當通道建立後,手機端應用可以接收webservice的任何資訊。

一起學Windows Phone 7開發(十二.Push Notification)

二.分類

對于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";

Message:

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

     <wp:Toast>

        <wp:Text1>

               <string>

        </wp:Text1>

        <wp:Text2>

        <string>

        </wp:Text2>

    </wp:Toast>

3.raw notification

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]

一起學Windows Phone 7開發(十二.Push Notification)
一起學Windows Phone 7開發(十二.Push Notification)
一起學Windows Phone 7開發(十二.Push Notification)
一起學Windows Phone 7開發(十二.Push Notification)

四.建立用戶端

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