首页 > 解决方案 > notificationPublisher.PublishAsync 未向租户发送通知

问题描述

我正在尝试向租户发送通知,但没有任何反应,甚至没有记录输入到“[AbpNotifications]”表中。我不知道哪里出了问题。

using (UnitOfWorkManager.Current.SetTenantId(tenantId))
{
    var notificationData = new LocalizableMessageNotificationData(new LocalizableString("OverDueTaskManagementReminderMessage", DConsts.LocalizationSourceName));
    notificationData["a"] = "Accomplish By" + ws.WorkStream.AccomplishOn.ToString("dd/MM/yyyy hh:mm");
    notificationData["pn"] = user.Surname + "" + user.Name;
    notificationData["tmp"] = WorkStreamPriority.Urgent.ToString();

    AsyncHelper.RunSync(() => _notificationPublisher.PublishAsync(AppNotificationNames.OverDueTaskManagementReminder,
        notificationData, severity: NotificationSeverity.Info));

    UnitOfWorkManager.Current.SaveChanges();
    return true;
}

在发布前订阅,代码如下,这次通知没有插入主机数据库,也没有插入租户数据库

await _notificationSubscriptionManager.SubscribeAsync(new UserIdentifier(tenantId, (long)(AbpSession.UserId??1)), AppNotificationNames.OverDueTaskManagementReminder);

            var result = _notificationPublisher.PublishAsync(AppNotificationNames.OverDueTaskManagementReminder,
                notificationData, severity: NotificationSeverity.Info, tenantIds: new[] { tenantId }.Select(x => (int?)Convert.ToInt32(x)).ToArray()).IsCompleted;
                return result;

标签: c#multi-tenantasp.net-boilerplate

解决方案


tenantIds显式传递给PublishAsync而不是UnitOfWorkManager.Current.SetTenantId.
这将向订阅用户发布通知,tenantIds而不是会话的租户。

AsyncHelper.RunSync(() => _notificationPublisher.PublishAsync(
    AppNotificationNames.OverDueTaskManagementReminder,
    notificationData,
    severity: NotificationSeverity.Info,
    tenantIds: new[] { tenantId } // Add this
));

解释

实现细节:如果两者tenantIdsuserIds都没有设置,则PublishAsync使用AbpSession.TenantId.

if (tenantIds.IsNullOrEmpty() && userIds.IsNullOrEmpty())
{
    tenantIds = new[] { AbpSession.TenantId };
}

推荐阅读