读书人

微软云技术Windows Azure专题(三):

发布时间: 2013-10-06 18:25:14 作者: rapoo

微软云技术Windows Azure专题(三):如何利用Mobile向Windows商店应用推送消息(2)

上一讲我们讲了如何利用Mobile向Windows商店应用推送消息。可是用心的朋友会发现,这个推送只能对自己当前的电脑有效,并不能推送给所有安装了这个应用的用户。

哈哈,其实这里边用到了通道的原理。当应用启动的时候,会向WindowsAzure服务注册当前机器和服务器的连接通道。然后Windows Azure服务根据通道,将信息发送给用户。

试想一下,如果没有通道,那么云服务就要搜索全球所有设备安装的所有应用,然后一个一个的判断是否为目标应用来进行下一步推送的话,那服务器可真受不了了。

下面进入正题:

如果想让Mobile Service给所有使用了当前应用的的设备发送推送的话,就需要记录下来每一台设备的通道。然后根据记录的通道逐个发送推送就行了。

先来明确一下大体上要做哪些步骤:

1.上一讲的所有操作。

2.建立一个通道表(Channel)

3.修改Windows商店应用的代码

4.修改Mobile Service的脚本

5.测试应用

第一步:上一讲的所有操作

不罗嗦了,大家自己到上一讲看吧

第二步:建立一个通道表(Channel)

1.进入Mobile Service面板

微软云技术Windows Azure专题(三):怎么利用Mobile向Windows商店应用推送消息(2)

2.点击数据,然后点击创建

微软云技术Windows Azure专题(三):怎么利用Mobile向Windows商店应用推送消息(2)

3.建立一个名为Channel的表,该表会自动在数据库中建立

微软云技术Windows Azure专题(三):怎么利用Mobile向Windows商店应用推送消息(2)

第三步:修改Windows商店应用的代码

1.将MainPage.xaml.cs中的TodoItem修改成

public class TodoItem{    public int Id { get; set; }[JsonProperty(PropertyName = "text")]public string Text { get; set; }[JsonProperty(PropertyName = "complete")]public bool Complete { get; set; }}


2.将MainPage.xaml.cs中的private void ButtonSave_Click(object sender, RoutedEventArgs e){ var todoItem = new TodoItem { Text = TextInput.Text }; InsertTodoItem(todoItem);}

3.在MainPage.xaml.cs中添加一个Channel类

public class Channel{    public int Id { get; set; }[JsonProperty(PropertyName = "uri")]public string Uri { get; set; }}

4.修改App.xaml.cs中的AcquirePushChannel函数

private async void AcquirePushChannel(){   CurrentChannel =        await PushNotificationChannelManager.CreatePushNotificationChannelForApplicationAsync();   IMobileServiceTable<Channel> channelTable = App.MobileService.GetTable<Channel>();   var channel = new Channel { Uri = CurrentChannel.Uri };   await channelTable.InsertAsync(channel);}


第三步:修改Mobile Service的脚本

1.点击数据中的Channel表

微软云技术Windows Azure专题(三):怎么利用Mobile向Windows商店应用推送消息(2)

2.修改表的插入函数

微软云技术Windows Azure专题(三):怎么利用Mobile向Windows商店应用推送消息(2)

3.将Channel的插入函数改为

function insert(item, user, request) {    var channelTable = tables.getTable('Channel');    channelTable        .where({ uri: item.uri })        .read({ success: insertChannelIfNotFound });    function insertChannelIfNotFound(existingChannels) {        if (existingChannels.length > 0) {            request.respond(200, existingChannels[0]);        } else {            request.execute();        }    }}

4.修改TodoItem表的插入函数

微软云技术Windows Azure专题(三):怎么利用Mobile向Windows商店应用推送消息(2)

5.将TodoItem的插入函数改为

function insert(item, user, request) {    request.execute({        success: function() {            request.respond();            sendNotifications();        }    });function sendNotifications() {    var channelTable = tables.getTable('Channel');    channelTable.read({        success: function(channels) {            channels.forEach(function(channel) {                push.wns.sendToastText04(channel.uri, {                    text1: item.text                }, {                    success: function(pushResponse) {                        console.log("Sent push:", pushResponse);                    }                });            });        }    });}}


第五步:测试应用

如果你有多台设备,并且都部署了该应用的话。你就会发现,每当你向表中插入数据的时候,所有的设备都将接收到右上角的Toast推送消息。

微软云技术Windows Azure专题(三):怎么利用Mobile向Windows商店应用推送消息(2)

微软云技术Windows Azure专题(三):怎么利用Mobile向Windows商店应用推送消息(2)


读书人网 >windows

热点推荐