Esempio n. 1
0
        /// <summary>
        /// 指定提供者发布通知
        /// </summary>
        /// <param name="providers">提供者列表</param>
        /// <param name="notificationInfo">通知信息</param>
        /// <returns></returns>
        protected async Task PublishFromProvidersAsync(IEnumerable <INotificationPublishProvider> providers,
                                                       NotificationInfo notificationInfo)
        {
            Logger.LogDebug($"Persistent notification {notificationInfo.Name}");

            // 持久化通知
            await NotificationStore.InsertNotificationAsync(notificationInfo);

            // TODO: 某些情况下,不能直接在服务内订阅消息,目前只能通过将订阅内容放进消息内部,需要重构通知系统设计了
            if (notificationInfo.Data.HasUserNotification(out Guid userId, out string userName))
            {
                await NotificationSubscriptionManager.SubscribeAsync(notificationInfo.TenantId,
                                                                     new UserIdentifier(userId, userName), notificationInfo.Name);
            }

            Logger.LogDebug($"Gets a list of user subscriptions {notificationInfo.Name}");
            // 获取用户订阅列表
            var userSubscriptions = await NotificationSubscriptionManager.GetSubscriptionsAsync(notificationInfo.TenantId, notificationInfo.Name);

            Logger.LogDebug($"Persistent user notifications {notificationInfo.Name}");
            // 持久化用户通知
            var subscriptionUserIdentifiers = userSubscriptions.Select(us => new UserIdentifier(us.UserId, us.UserName));

            await NotificationStore.InsertUserNotificationsAsync(notificationInfo,
                                                                 subscriptionUserIdentifiers.Select(u => u.UserId));

            // 发布通知
            foreach (var provider in providers)
            {
                await PublishAsync(provider, notificationInfo, subscriptionUserIdentifiers);
            }

            if (notificationInfo.Lifetime == NotificationLifetime.OnlyOne)
            {
                // 一次性通知在发送完成后就取消用户订阅
                await NotificationStore.DeleteAllUserSubscriptionAsync(notificationInfo.TenantId,
                                                                       notificationInfo.Name);
            }
        }
Esempio n. 2
0
        public override async Task PublishAsync(NotificationInfo notification, IEnumerable <UserIdentifier> identifiers)
        {
            // step1 默认微信openid绑定的就是username,
            // 如果不是,需要自行处理openid获取逻辑

            // step2 调用微信消息推送接口

            // 微信不支持推送到所有用户,需要获取订阅列表再发送
            // 在小程序里用户订阅消息后通过 api/subscribes/subscribe 接口订阅对应模板消息
            if (identifiers == null)
            {
                var userSubscriptions = await NotificationSubscriptionManager
                                        .GetSubscriptionsAsync(notification.TenantId, notification.Name);

                identifiers = userSubscriptions
                              .Select(us => new UserIdentifier(us.UserId, us.UserName));
            }
            foreach (var identifier in identifiers)
            {
                await SendWeChatTemplateMessagAsync(notification, identifier);
            }
        }