前言
隨着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>0? args[0]:"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
