Windows Phone & Windows 8 Push Notification from Windows Azure


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

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

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

Untitled

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

隨后會彈出創建 Mobile Service 的向導, 輸入你的URL指向,以及數據庫連接,最后一個選項是選擇你的數據中心的位置。

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

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

image

 

image

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

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

 

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

image

 

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

image

對於推送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 還支持土司消息的推送。

image

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(推送標簽欄中)。

image

當然我們的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); 
                } 
    }); 
           } 
                
             }); 
             
     
}});

}

 image

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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM