Windows Phone & Windows 8 Push Notification from Windows Azure

來源:互聯網
上載者:User

相信大家多多少少已經對 Windows Azure 雲平台有所耳聞,現在的互連網已經進入雲+端的時代,我們手中的 PC 平板 手機 對網路的依賴程度日益深入,尤其是一些社交類型的應用更是需要一些資訊的推送,之前我給大家介紹過關於windows phone 的推送服務,今天主要給大家介紹一下 基於微軟雲平台的手機推送服務。

首先使用Mobile service除了要安裝我們的VS2012 + WP8 SDK以外 還要安裝 Mobile Services SDK 

首先我們要登入 Management Portal Windows Azure的管理頁面(當然你已經有一個 Windows Azure的訂閱)。

可以看到左側的 Mobile service 或者點擊左下角的添加按鈕 選擇建立一個新的Mobile service.

隨後會彈出建立 Mobile Service 的嚮導, 輸入你的URL指向,以及資料庫連接,最後一個選項是選擇你的資料中心的位置。

當然這裡如果你選擇的是使用一個新的資料庫 會要求輸入資料庫名稱和 登入名稱稱和密碼. 點擊完成按鈕

隨後你可以在Mobile Service的選項下看到你剛建立的服務.

 

隨後你可以選擇下載一個程式碼範例項目或者將你已有的一個項目添加到Mobile Service中,我這裡直接選擇下載Windows Azure的 Demo Code.

運行你的項目發現已經可以和Mobile Service進行資料互動了, 是不是很簡單?

 

在我們的服務中可以直接瀏覽到資料表中的資料.

 

當然這裡也有 Win8 版本的demo code下載。

對於推送Windows Phone是這樣的 用戶端和之前沒什麼太多區別還是要註冊手機推送通道.

在Manifest檔案中標記推送

在手機App檔案中添加以下代碼

1. 引入命名空間

using Microsoft.Phone.Notification;

 

2. 添加以下代碼

public static HttpNotificationChannel CurrentChannel { get; private set; }private void AcquirePushChannel(){    CurrentChannel = HttpNotificationChannel.Find("MyPushChannel");if (CurrentChannel == null){    CurrentChannel = new HttpNotificationChannel("MyPushChannel");    CurrentChannel.Open();    CurrentChannel.BindToShellTile();}}

 

3. 在Application_Launching事件方法中添加方法調用

AcquirePushChannel(); 

4.在TodoItem類中添加一個欄位

[DataMember(Name = "channel")] public string Channel { get; set; }

 

5. 最後在MainPage頁面中更改ButtonSave_Click事件響應代碼

private void ButtonSave_Click(object sender, RoutedEventArgs e){    var todoItem = new TodoItem { Text = TodoInput.Text,         Channel = App.CurrentChannel.ChannelUri.ToString() };    InsertTodoItem(todoItem);}

 

在 Windows Azure 雲端我們要編輯下插入資料時的指令碼代碼

選擇Data(資料) – Script (指令碼) – Insert(插入)

更新代碼如下:

function insert(item, user, request) {    request.execute({        success: function () {            // Write to the response and then send the notification in the background            request.respond();            push.mpns.sendFlipTile(item.channel, {                title: item.text            }, {                success: function (pushResponse) {                    console.log("Sent push:", pushResponse);                }            });        }    });}

 

此時我們部署我們的項目到模擬器或者手機並且把我們應用的Tile ping到案頭上.

插入一條資料後,檢查我們的Tile表徵圖已經推送了一條訊息過來了。

以上其實是WindowsAzure網站上的一個快速指導 我給大家搬過來加以總結, 不過我想相信大家不僅僅是使用Tile的推送這裡Mobile 還支援土司訊息的推送。

Mobile Service 不僅僅支援 Windows Phone 同樣支援 windows 8 的訊息推送 ,下面我介紹下如何配置Windows 8 的Mobile service訊息推送。

這裡我就用上面 Windows Azure剛剛建立的TodoList表不在單獨建立資料庫。同樣可以從Windows Azure網站上下載 Windows 8的 DEMO 範例程式碼稍加修改就可以支援我們的Windows 8 訊息推送了。

Windows 8 的註冊要比Windows Phone負責一點,要在Windows 市集先註冊並且拿到你的 應用推送的 CLIENT SECRET 和 PACKAGE SID 操作如下:

首先你要先登入 Submit an app page 註冊你的Win8應用並且在給你的應用預留一個應用程式名稱.

隨後在VS中關聯市集中的應用

接著在 Windows dev Center 中選擇 Advanced features

選擇 Authenticating your service 並且記錄下  Client secret and Package security identifier (SID).

將記錄的ID上傳到Windows Azure中的 push(推送標籤欄中)。

當然我們的Windwos 8 應用也要聲明支援推送服務

1. 使用命名空間

using Windows.Networking.PushNotifications;

 

2. App檔案中添加代碼

public static PushNotificationChannel CurrentChannel { get; private set; }private async void AcquirePushChannel(){        CurrentChannel =              await PushNotificationChannelManager.CreatePushNotificationChannelForApplicationAsync();}

 

3. OnLaunched 事件中添加

AcquirePushChannel();

 

4. TodoItem類中添加屬性

[DataMember(Name = "channel")] public string Channel { get; set; }

 

5. 在MainPage中的ButtonSave_Click事件中添加代碼

private void ButtonSave_Click(object sender, RoutedEventArgs e)    {        var todoItem = new TodoItem { Text = TextInput.Text, Channel = App.CurrentChannel.Uri };        InsertTodoItem(todoItem);    }

另外我這裡更新Windows Azure插入資料指令碼。我這裡是插入資料時推送所有裝置資訊(Win8 & windows Phone 土司訊息)

function insert(item1, user, request) {    request.execute();         var permissionsTable = tables.getTable('todoitem');     permissionsTable.where(function(){     return this.channel !== null }).read({ success: function(channels) {      console.log("read data:", channels.length );           var pushString= 'Hello';             channels.forEach(function(item) {            if(item.iswin8data === true){      push.wns.sendToastText04(item.channel, {                 text1: item1.text             }, {                 success: function(pushResponse) {                     console.log("Sent push:", pushResponse);                 }             });            }else{                    push.mpns.sendToast(item.channel, {         text1: item1.text,         test2: new Date().toISOString().replace(/T/, ' ').replace(/\..+/, ''),         param: '/Page2.xaml?NavigatedFrom=Toast Notification'     }, {        success: function (pushResponse) {                     console.log("Sent toast push sucess:",pushResponse+" "+item.channel);                 },                    error: function (failedResponse){                     console.log("Sent toast push fail:", failedResponse);                 }     });            }                              });                   }});}

 

 歡迎大家在這裡和我溝通交流或者在新浪微博上 @王博_Nick

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.