如今,企业正逐步向最终用户(外部)或员工(内部)创建移动应用程序。 它们现有的后端系统是大型机或某些 LoB 应用程序,这些应用程序必须集成到移动应用程序体系结构中。 本指南介绍了如何最好地实现此集成,建议为常见方案提供可能的解决方案。
频繁要求是在后端系统中发生相关事件时,通过移动应用程序向用户发送推送通知。 例如,拥有银行银行应用程序的 iPhone 用户希望在从账户中扣款超过某一额度时收到通知;而在另一个场景中,作为内部网环境的一部分,财务部门的一名员工在其 Windows Phone 上安装了预算审批应用程序,希望在收到审批请求时能得到通知。
银行帐户或审批处理可能在某些后端系统中完成,这必须向用户发起推送。 可能有多个此类后端系统,这些系统必须在事件触发通知时生成相同的逻辑进行推送。 此处的复杂性在于将多个后端系统与单个推送系统集成,最终用户可能订阅了不同的通知,甚至可能有多个移动应用程序。 例如,一个移动应用程序可能需要从多个此类后端系统接收通知的 Intranet 移动应用。 后端系统不知道或需要知道推送语义/技术,因此,这里的常见解决方案是引入一个组件,该组件轮询后端系统是否有任何感兴趣的事件,并负责将推送消息发送到客户端。
更好的解决方案是使用 Azure 服务总线 - 主题/订阅模型,这可以降低复杂性,同时使解决方案可缩放。
下面是解决方案的一般体系结构(使用多个移动应用通用化,但当只有一个移动应用时同样适用)
Architecture
此体系结构关系图中的关键部分是 Azure 服务总线,它提供主题/订阅编程模型(可在 Service Bus Pub/Sub 编程中了解更多)。 在这种情况下,接收方是移动后端(通常是启动推送到移动应用的 Azure 移动服务)不会直接从后端系统接收消息,而是 Azure 服务总线提供的中间抽象层,它使移动后端能够接收来自一个或多个后端系统的消息。 需要为每个后端系统创建服务总线主题,例如帐户、人力资源、财务,这基本上是指“兴趣主题”,用以启动以推送通知形式发送的消息。 后端系统向这些主题发送消息。 移动后端可以通过创建服务总线订阅来订阅一个或多个此类主题。 它授权移动后端从相应的后端系统接收通知。 移动后端继续侦听其订阅上的消息,一旦消息到达,它就会将其作为通知发送到其通知中心。 然后,通知中心最终会将消息传送到移动应用。 下面是关键组件的列表:
- 后端系统(LoB/传统系统)
- 创建服务总线主题
- 发送消息
- 移动后端
- 创建服务订阅
- 接收消息(来自后端系统)
- 通过 Azure 通知中心将通知发送到客户端
- 移动应用程序
- 接收和显示通知
优点
- 接收者(通过通知中心的移动应用/服务)与发送者(后端系统)之间的解耦,使得其他后端系统能够以最小的变更进行集成。
- 它还使多个移动应用能够从一个或多个后端系统接收事件。
Sample
先决条件
完成以下教程,熟悉概念以及常见的创建和配置步骤:
- 服务总线发布/子编程 - 本教程介绍了如何使用服务总线主题/订阅、如何创建包含主题/订阅的命名空间、如何从主题/订阅发送和接收消息的详细信息。
- 通知中心 - Windows 通用教程 - 本教程介绍如何设置 Windows 应用商店应用并使用通知中心注册并接收通知。
代码示例
通知中心示例提供了完整的示例代码。 它分为三个组件:
EnterprisePushBackendSystem
a。 此项目使用 Azure.Messaging.ServiceBus NuGet 包,并基于 服务总线 Pub/Sub 编程。
b. 此应用程序是一个简单的 C# 控制台应用,用于模拟 LoB 系统,它启动要传递到移动应用的消息。
static async Task Main(string[] args) { string connectionString = ConfigurationManager.AppSettings.Get("Azure.ServiceBus.ConnectionString"); // Create the topic await CreateTopicAsync(connectionString); // Send message await SendMessageAsync(connectionString); }c.
CreateTopicAsync用于创建服务总线主题。public static async Task CreateTopicAsync(string connectionString) { // Create the topic if it does not exist already ServiceBusAdministrationClient client = new ServiceBusAdministrationClient(connectionString); if (!await client.TopicExistsAsync(topicName)) { await client.CreateTopicAsync(topicName); } }d.
SendMessageAsync用于将消息发送到此服务总线主题。 此代码只是为了示例目的定期向主题发送一组随机消息。 通常有一个后端系统,在事件发生时发送消息。public static sync Task SendMessageAsync(string connectionString) { await using var client = new ServiceBusClient(connectionString); ServiceBusSender sender = client.CreateSender(topicName); // Sends random messages every 10 seconds to the topic string[] messages = { "Employee Id '{0}' has joined.", "Employee Id '{0}' has left.", "Employee Id '{0}' has switched to a different team." }; while (true) { Random rnd = new Random(); string employeeId = rnd.Next(10000, 99999).ToString(); string notification = String.Format(messages[rnd.Next(0,messages.Length)], employeeId); // Send Notification ServiceBusMessage message = new ServiceBusMessage(notification); await sender.SendMessageAsync(message); Console.WriteLine("{0} Message sent - '{1}'", DateTime.Now, notification); System.Threading.Thread.Sleep(new TimeSpan(0, 0, 10)); } }ReceiveAndSendNotification
a。 此项目使用 Azure.Messaging.ServiceBus 和 Microsoft.Web.WebJobs.Publish NuGet 包,并且基于 服务总线 Pub/Sub 编程。
b. 以下控制台应用作为 [Azure WebJob] 运行,因为它必须持续运行才能侦听来自 LoB/后端系统的消息。 此应用程序是移动后端的一部分。
static async Task Main(string[] args) { string connectionString = ConfigurationManager.AppSettings.Get("Azure.ServiceBus.ConnectionString"); // Create the subscription that receives messages await CreateSubscriptionAsync(connectionString); // Receive message await ReceiveMessageAndSendNotificationAsync(connectionString); }c.
CreateSubscriptionAsync用于为后端系统发送消息的主题创建服务总线订阅。 根据业务方案,此组件会创建一个或多个相应主题的订阅(例如,有些订阅可能从 HR 系统接收消息、部分来自财务系统等)static async Task CreateSubscriptionAsync(string connectionString) { // Create the subscription if it does not exist already ServiceBusAdministrationClient client = new ServiceBusAdministrationClient(connectionString); if (!await client.SubscriptionExistsAsync(topicName, subscriptionName)) { await client.CreateSubscriptionAsync(topicName, subscriptionName); } }d.
ReceiveMessageAndSendNotificationAsync用于使用其订阅从主题中读取消息,如果读取成功,则创建通知(在示例方案中,Windows 本机 Toast 通知)以使用 Azure 通知中心发送到移动应用程序。static async Task ReceiveMessageAndSendNotificationAsync(string connectionString) { // Initialize the Notification Hub string hubConnectionString = ConfigurationManager.AppSettings.Get ("Microsoft.NotificationHub.ConnectionString"); hub = NotificationHubClient.CreateClientFromConnectionString (hubConnectionString, "enterprisepushservicehub"); ServiceBusClient Client = new ServiceBusClient(connectionString); ServiceBusReceiver receiver = Client.CreateReceiver(topicName, subscriptionName); // Continuously process messages received from the subscription while (true) { ServiceBusReceivedMessage message = await receiver.ReceiveMessageAsync(); var toastMessage = @"<toast><visual><binding template=""ToastText01""><text id=""1"">{messagepayload}</text></binding></visual></toast>"; if (message != null) { try { Console.WriteLine(message.MessageId); Console.WriteLine(message.SequenceNumber); string messageBody = message.Body.ToString(); Console.WriteLine("Body: " + messageBody + "\n"); toastMessage = toastMessage.Replace("{messagepayload}", messageBody); SendNotificationAsync(toastMessage); // Remove message from subscription await receiver.CompleteMessageAsync(message); } catch (Exception) { // Indicate a problem, unlock message in subscription await receiver.AbandonMessageAsync(message); } } } } static async void SendNotificationAsync(string message) { await hub.SendWindowsNativeNotificationAsync(message); }e. 若要将此应用发布为 WebJob,请右键单击 Visual Studio 中的解决方案,然后选择“发布为 Web 作业”
f. 选择您的发布配置文件,如果 Azure 网站尚不存在,则创建一个新的,以托管此 WebJob,然后在获得网站后 发布。
“发布 Web”对话框的屏幕截图,其中选择了“Azure 网站”选项,绿色箭头指向“选择现有网站”对话框,其中“新建”选项以红色显示,绿色箭头指向 Azure 上的“创建站点”对话框,其中“站点名称”和“创建”选项以红色边框。
g. 将作业配置为“持续运行”,以便在登录到 Azure 门户 时,应看到如下所示的内容:
EnterprisePushMobileApp
a。 此应用程序是 Windows 应用商店应用程序,它从作为移动后端的一部分运行的 WebJob 接收 Toast 通知并显示它。 此代码基于 通知中心 - Windows 通用教程。
b. 确保启用应用程序以接收 Toast 通知。
c. 确保在应用启动时调用以下通知中心注册代码(在替换
HubName和DefaultListenSharedAccessSignature值后)。private async void InitNotificationsAsync() { var channel = await PushNotificationChannelManager.CreatePushNotificationChannelForApplicationAsync(); var hub = new NotificationHub("[HubName]", "[DefaultListenSharedAccessSignature]"); var result = await hub.RegisterNativeAsync(channel.Uri); // Displays the registration ID so you know it was successful if (result.RegistrationId != null) { var dialog = new MessageDialog("Registration successful: " + result.RegistrationId); dialog.Commands.Add(new UICommand("OK")); await dialog.ShowAsync(); } }
运行示例
确保您的 WebJob 正在成功运行,并已设置为持续运行。
运行启动 Windows 应用商店应用的 EnterprisePushMobileApp。
运行 EnterprisePushBackendSystem 控制台应用程序,该应用程序模拟 LoB 后端并开始发送消息,应会看到 Toast 通知如下图所示:
这些消息最初被发送到 Service Bus 主题,而 Web 作业中的 Service Bus 订阅在对其进行监控。 收到消息后,将创建通知并将其发送到移动应用。 在 Azure 门户中 查看 Web 作业的“日志”链接时,可以查看 WebJob 日志以确认处理: