上一讲我们讲了如何利用Mobile向Windows商店应用推送消息。可是用心的朋友会发现,这个推送只能对自己当前的电脑有效,并不能推送给所有安装了这个应用的用户。
哈哈,其实这里边用到了通道的原理。当应用启动的时候,会向WindowsAzure服务注册当前机器和服务器的连接通道。然后Windows Azure服务根据通道,将信息发送给用户。
试想一下,如果没有通道,那么云服务就要搜索全球所有设备安装的所有应用,然后一个一个的判断是否为目标应用来进行下一步推送的话,那服务器可真受不了了。
下面进入正题:
如果想让Mobile Service给所有使用了当前应用的的设备发送推送的话,就需要记录下来每一台设备的通道。然后根据记录的通道逐个发送推送就行了。
先来明确一下大体上要做哪些步骤:
1.上一讲的所有操作。
2.建立一个通道表(Channel)
3.修改Windows商店应用的代码
4.修改Mobile Service的脚本
5.测试应用
第一步:上一讲的所有操作
不罗嗦了,大家自己到上一讲看吧
第二步:建立一个通道表(Channel)
1.进入Mobile Service面板
2.点击数据,然后点击创建
3.建立一个名为Channel的表,该表会自动在数据库中建立
第三步:修改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中的ButtonSave函数修改成
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表
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表的插入函数
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 mvp 的技术实践和心得体会,主要包括windows azure 平台的基础知识、使用方法、功能特点以及如何基于windows azure 平台设计高可靠、可扩展的应用程序,几乎涵盖...
微软云计算平台技术详解》来自于微软 Windows Azure MVP 的技术实践和心得体会,主要包括Windows Azure 平台的基础知识、使用方法、功能特点以及如何基于Windows Azure 平台设计高可靠、可扩展的应用程序,几乎涵盖...
了解微软的云平台Microsoft windows Azure for ISV
将带来Windows Azure平台的功能以及开发的方式等各式各样的信息,帮助您了解微软的云计算平台以及相关技术,或是评估微软的Windows Azure平台能力,以及如何在Windows Azure上开发应用程序的知识和技术。 全书共12章...
微软公有云Azure 教程
Windows Azure 表存储:不同于传统数据库.pdf
微软WindowsAzure云应用开发实践整理.pdf
实战 Windows Azure:微软云计算平台技术详解
《云计算(第二版)》教材配套课件[PPT] 第四章 微软云计算Windows Azure(2).ppt
微软云课件(共9课) 1、认识云计算 2、为什么选择Windows Azure 3、第一次使用Windows Azure必读 4、第一课:创建虚拟机 5、第二课:建立自己的网站 6、第三课:创建云端的数据库 7、第四课:开发测试在云中 8、第...
实战WindowsAzure:微软云计算平台技术详解(目录)实战WindowsAzure:微软云计算平台技术详解(目录)
Learning Windows Azure Mobile Services for Windows 8 and Windows Phone 8 A short, fast and focused guide to enhance your Windows 8 applications by leveraging the power of Windows Azure Mobile ...
Windows Azure是微软基于云计算的操作系统,现在更名为“Microsoft Azure”,和Azure Services Platform一样,是微软“软件和服务”技术的名称。Windows Azure的主要目标是为开发者提供一个平台,帮助开发可运行在云...
Windows Azure
《WINDOWS AZURE》产品介绍——微软官方windows8风格动画ppt模板
Windows Azure使用入门 第二课:建立自己的网站.pdf
Windows Azure是一个开放的云平台,在Azure之上不仅可以运行Windows操作系统和各种微软的软件产品(如SQL Server,Sharepoint等),还可以运行各种开源软件,例如Linux,PHP,MySQL,Apache, Nodejs, MongoDB等。...
《实战windows azure 微软云计算平台技术详解》。
简要描述Windows Azure最新的功能、特色和主要的服务
微软Windows Azure云计算平台