前言

随着Windows Azure 在中国的正式落地,相信越来越多的人会体验到Windows Azure带来的强大和便利。在上一篇文章中, 我们介绍了如何利用Windows Azure中的Service Bus的中继功能(Relay),来将您的WCF服务册到Windows Azure Service Bus上。 而WCF客户端可以通过访问Service Bus endpoint, 从而访问到您真正的WCF服务。
在这篇文章中,我们将介绍Windows Azure Service Bus的另外一个功能:通知中心(Notification Hub)。 (说明: 该功能目前还处于Preview阶段)
在很多应用系统中,通过Publisher |Subscriber 模型 来实现消息推送是一个常用的功能。实现方法也有很多,且较为复杂。幸运的是,在Windows Azure Service Bus中, 我们提供一个功能:通知中心(Notification Hub), 可以非常方便地实现大批量的推送通知的功能。
下面, 我就以开发一个简单的Windows 8 application为例,介绍如何利用这个功能实现Toast notification。

前提条件

1. 开发环境:Visual Studio 2012 + Windows 8
2. Windows Azure 账号 (您可以通过以下地址免费申请一个: http://www.azure.com)
3. Windows 8开发者账号。
4. Service Bus WinRT Preview SDK (http://go.microsoft.com/fwlink/?LinkID=277160)

正文

第一部分:配置

1. 首先, 您可以创建一个用于演示的Windows Store application。为了接收推送下来的Toast Notification,您必须先要开启该功能, 如下所示:

2. 然后,将该application和Windows Store关联起来,并在Windows Store里面正确配置该application, 比如Notification等等,并获取正确的Package Security Identifier (SID) 和Client secret。

里面最关键的一步,是要配置你的application的推送通知的功能,并获得相应的Package Security Identifier (SID) 和Client secret。
比如:下面就是我配置后生成的相应SID和Client secret:

3. 接下来, 我们需要回到Windows Azure  Management Portal, 创建一个Notification Hub。 比如,我在namesapce4push下,创建了一个叫做myhub的 Notification Hub,如下所示:

4. 然后,给刚才创建的Notification Hub : myhub 指定相应的Package SID和Client Secret,使得我们自己开发的application和该notification hub关联起来,如下所示:

最后点击保存。

5. 上述设置结束之后,回到Visual Studio,其会自动检测到这一行为,点击下一步直至结束,这样该application和store成功关联起来了。

第二部分:编写代码

1. 在application里面,我们需要在程序启动的时候,完成一些初始化的动作,比如该application在Notification Hub的注册等。
下面用黄色高亮显示的部分为相关代码:

using PushDemo.Common;

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Windows.ApplicationModel;
using Windows.ApplicationModel.Activation;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
using System.Threading.Tasks; using Microsoft.WindowsAzure.Messaging;
using Windows.Networking.PushNotifications;
using Windows.UI.Notifications;
using Windows.Data.Xml.Dom; // The Grid App template is documented at http://go.microsoft.com/fwlink/?LinkId=234226 namespace PushDemo
{
/// <summary>
/// Provides application-specific behavior to supplement the default Application class.
/// </summary>
sealed partial class App : Application
{
/// <summary>
/// Initializes the singleton Application object. This is the first line of authored code
/// executed, and as such is the logical equivalent of main() or WinMain().
/// </summary>
/// NotificationHub notificationHub; public App()
{
var cn = ConnectionString.CreateUsingSharedAccessSecretWithListenAccess(
new Uri("sb://namespace4push.servicebus.windows.net/"),
"******<listenAccessSecret>******"
); notificationHub = new NotificationHub("myhub", cn); this.InitializeComponent();
this.Suspending += OnSuspending;
} async Task InitializeNotificationsAsync()
{
var channel = await PushNotificationChannelManager.CreatePushNotificationChannelForApplicationAsync();
await notificationHub.RefreshChannelUriAsync(channel.Uri); if (!await notificationHub.TemplateRegistrationExistsAsync("myToastRegistration"))
await notificationHub.CreateTemplateRegistrationAsync(BuildTextToastTemplate(), "myToastRegistration");
} XmlDocument BuildTextToastTemplate()
{
var template =
ToastNotificationManager.GetTemplateContent(ToastTemplateType.ToastText01);
var textNode = template.SelectSingleNode("//text[@id='1']") as XmlElement;
if (textNode != null)
{
textNode.InnerText = "$(msg)";
}
return template;
} /// <summary>
/// Invoked when the application is launched normally by the end user. Other entry points
/// will be used when the application is launched to open a specific file, to display
/// search results, and so forth.
/// </summary>
/// <param name="args">Details about the launch request and process.</param>
protected override async void OnLaunched(LaunchActivatedEventArgs args)
{
await InitializeNotificationsAsync(); Frame rootFrame = Window.Current.Content as Frame; // Do not repeat app initialization when the Window already has content,
// just ensure that the window is active if (rootFrame == null)
{
// Create a Frame to act as the navigation context and navigate to the first page


}
// Ensure the current window is active
Window.Current.Activate();
} /// <summary>
/// Invoked when application execution is being suspended. Application state is saved
/// without knowing whether the application will be terminated or resumed with the contents
/// of memory still intact.
/// </summary>
/// <param name="sender">The source of the suspend request.</param>
/// <param name="e">Details about the suspend request.</param>


}
}

2. 接下来我们需要一个用来推送消息的后台程序。我们就用一个非常简单的windows console程序来模拟后台好了。相关的代码如下:

class Program
{
static void Main(string[] args)
{
var cn = ServiceBusConnectionStringBuilder.CreateUsingSharedAccessKey(
new Uri("sb://namespace4push.servicebus.windows.net/"),
Microsoft.ServiceBus.Notifications.NotificationHubDescription.DefaultFullSasRuleName,
"*****<Access key>********”
); var hbClient = Microsoft.ServiceBus.Notifications.NotificationHubClient.CreateClientFromConnectionString(cn, "myhub");
hbClient.SendTemplateNotification(new Dictionary<string, string>
{
{"msg",args.Length>? args[]:"Hello World, Service Bus Notification Hub"}
}
);
}
}

3. 运行后的效果如下:
1)首先, 我们用我们的模拟后台消息推送程序推送一条消息给Notification Hub,如下:

2)因为我们的Windows Store Application注册到了Service Bus Notification Hub。 因此,所有安装该application的终端都会马上收到一个Toast Notification,如下:

也就是说, 假设我们有1万个终端的话, 那么这一万个终端会同时收到该Toast Notification.
说明:在Notification Hub Preview阶段,目前一个Notification Hub最多支持10,000个注册。如果需要超过1万个的注册,需要和我们的产品组联系以便特殊处理。

参考文档:

1) How To: Service Bus Notification Hubs (Windows Store Apps)
http://msdn.microsoft.com/en-us/library/windowsazure/jj927172.aspx
2) Creating Windows Store Apps with Windows Azure Notification Hubs
http://blogs.msdn.com/b/zxue/archive/2013/01/24/creating-windows-store-apps-with-windows-azure-notification-hubs.aspx

希望以上内容对您有所帮助

Winston He

  

Windows Azure Service Bus Notification Hub推送通知的更多相关文章

  1. 与众不同 windows phone (10) - Push Notification(推送通知)之推送 Tile 通知, 推送自定义信息

    原文:与众不同 windows phone (10) - Push Notification(推送通知)之推送 Tile 通知, 推送自定义信息 [索引页][源码下载] 与众不同 windows ph ...

  2. 与众不同 windows phone (9) - Push Notification(推送通知)之概述, 推送 Toast 通知

    原文:与众不同 windows phone (9) - Push Notification(推送通知)之概述, 推送 Toast 通知 [索引页][源码下载] 与众不同 windows phone ( ...

  3. Windows Azure Service Bus Topics实现系统松散耦合

    前言 Windows Azure中的服务总线(Service Bus)提供了多种功能, 包括队列(Queue), 主题(Topic),中继(Relay),和通知中心(Notification Hub) ...

  4. Windows Azure Service Bus (2) 队列(Queue)入门

    <Windows Azure Platform 系列文章目录> Service Bus 队列(Queue) Service Bus的Queue非常适合分布式应用.当使用Service Bu ...

  5. Windows Azure Service Bus (3) 队列(Queue) 使用VS2013开发Service Bus Queue

    <Windows Azure Platform 系列文章目录> 在之前的Azure Service Bus中,我们已经介绍了Service Bus 队列(Queue)的基本概念. 在本章中 ...

  6. Windows Azure Service Bus (4) Service Bus Queue和Storage Queue的区别

    <Windows Azure Platform 系列文章目录> 熟悉笔者文章的读者都了解,Azure提供两种不同方式的Queue消息队列: 1.Azure Storage Queue 具体 ...

  7. Windows Azure Service Bus (5) 主题(Topic) 使用VS2013开发Service Bus Topic

    <Windows Azure Platform 系列文章目录> 项目文件,请在这里下载 在笔者之前的文章中Windows Azure Service Bus (1) 基础 介绍了Servi ...

  8. 重新想象 Windows 8 Store Apps (67) - 后台任务: 推送通知

    [源码下载] 重新想象 Windows 8 Store Apps (67) - 后台任务: 推送通知 作者:webabcd 介绍重新想象 Windows 8 Store Apps 之 后台任务 推送通 ...

  9. Windows Azure Service Bus (6) 中继(Relay On) 使用VS2013开发Service Bus Relay On

    <Windows Azure Platform 系列文章目录> 注意:本文介绍的是国内由世纪互联运维的Windows Azure服务. 项目文件请在这里下载. 我们在使用Azure平台的时 ...

随机推荐

  1. Shell文本处理 - 分割合并与过滤

    sort分类操作 示例文件 Boys in Company C:HK:192:2192 Alien:HK:119:1982 The Hill:KL:63:2972 Aliens:HK:532:4892 ...

  2. jetbrains产品激活方式(WebStorm,Pycharm有效)

    注册时,在打开的License Activation窗口中选择"activation code",在输入框输入下面的注册码 43B4A73YYJ-eyJsaWNlbnNlSWQiO ...

  3. My97Datepicker 去掉 “不合法格式或超期范围”自动纠错限制

    官网上,纠错有以下三种,如日期格式不对,或超期,则必须纠错过后,才能继续操作, 但有时,可能允许出错,需要把纠错功能去掉,则可以设置errDealMode = 3,这种模式是官网说没有的, 但能够去掉 ...

  4. Jquery常用radio取值,checkbox取值,select取值,radio选中,checkbox选中,select选中,及其相关设置

    获取一组radio被选中项的值:var item = $('input[name=items][checked]').val(); 获取select被选中项的文本:var item = $(" ...

  5. Nodejs 高并发长链接TCP链接的服务器设计问题

    最近有个项目比较棘手,nodejs的tcp服务,目前的服务器支持3W左右的客户端连接,但是客户希望能够支持30W左右,原先的模型是让客户端请求一个地址分发服务器,然后再tcp链接到不同的地址上实现高并 ...

  6. C#中语音合成简单使用

    我使用的是vs2013 1.在项目中添加引用,项目->添加引用->COM选择Microsoft Speech Object Library 2.在需要使用语音合成的地方调用代码: SpVo ...

  7. BruteXSS:XSS暴力破解神器

    ×01 BruteXSS BruteXSS是一个非常强大和快速的跨站点脚本暴力注入.它用于暴力注入一个参数.该BruteXSS从指定的词库加载多种有效载荷进行注入并且使用指定的载荷和扫描检查这些参数很 ...

  8. BNUOJ 51279[组队活动 Large](cdq分治+FFT)

    传送门 大意:ACM校队一共有n名队员,从1到n标号,现在n名队员要组成若干支队伍,每支队伍至多有m名队员,求一共有多少种不同的组队方案.两个组队方案被视为不同的,当且仅当存在至少一名队员在两种方案中 ...

  9. 一款免费支持PDF、word、excel、PPT、jpeg之间互转线上软件

    偶然发现的一款免费支持PDF.word.excel.PPT.jpeg之间互转,支持合并pdf.加密解密PDF的线上软件,首先声明,不是广告党,我自己试用过,确实是目前我用过最好用的,如果有朋友有更好的 ...

  10. NY 325 zb的生日

    假设所有西瓜重 Asum,所求的是用 Asum / 2 的背包装,最多装下多少. 刚开始用贪心作的,WA.后来用01背包,结果TLE,数据太大.原来用的是深搜! dfs(int sum, int i) ...