Windows Phone7開發之 Push+Notification

webphp 12年前發布 | 21K 次閱讀 Windows Phone開發 移動開發 Windows Phone

一.簡介

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里有。

 本文由用戶 webphp 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!