Search code examples
c#multi-tenantasp.net-boilerplate

notificationPublisher.PublishAsync is not sending notification to tenant


I am trying to send a notification to the tenant but nothing happens, not even record entered to the '[AbpNotifications]' table. I don't know where things went wrong.

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;
}

Subscribed before publish and the code as below, this time notification didn't insert in host db nor in tenant db

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;

Solution

  • Pass tenantIds explicitly to PublishAsync instead of UnitOfWorkManager.Current.SetTenantId.
    This will publish notifications to subscribed users in the tenantIds instead of the session's tenant.

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

    Explanation

    Implementation detail: if both tenantIds and userIds are not set, then PublishAsync uses AbpSession.TenantId.

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