【问题标题】:Unable to send real-time notifications saved in database无法发送保存在数据库中的实时通知
【发布时间】:2019-03-18 23:30:14
【问题描述】:

我正在使用 ASP.NET Boilerplate 模板并尝试为特定用户获取基于实体的实时通知。我的通知成功发布在Abp.TenantNotifications 表中,订阅成功保存在Abp.NotificationSubscriptions 表中。但我无法让它显示在客户端。

我已根据the documentation将我的项目与SignalR集成。


我发布通知的应用服务

public async Task PublishCreated(Guid WorkOrderId, long UserId)
{
    var user = await UserManager.GetUserByIdAsync(UserId);

    var workorder = await _workOrderDirectoryRepository.GetAsync(WorkOrderId);

    await _notiticationPublisher.PublishAsync(
        notificationName: "WorkOrder",
        data: new MessageNotificationData("New WO is " + workorder.WorkOrderStatus + " with code " + workorder.WorkOrderCode),
        entityIdentifier: new EntityIdentifier(typeof(WorkOrderDirectory), workorder.Id),
        severity: NotificationSeverity.Success,
        userIds: new[] { user.ToUserIdentifier() }
    );

    await _notificationSubscriptionManager.SubscribeAsync(user.ToUserIdentifier(), "WorkOrder");

    await _backgroundJobManager.EnqueueAsync<WONotificationJob, UserIdentifier>(
        user.ToUserIdentifier(),
        delay: TimeSpan.FromSeconds(5)
    );
}

后台工作

public class WONotificationJob : BackgroundJob<UserIdentifier>, ITransientDependency
{
    private readonly IRealTimeNotifier _realTimeNotifier;
    private readonly IUserNotificationManager _userNotificationManager;

    public WONotificationJob(
        IRealTimeNotifier realTimeNotifier,
        IUserNotificationManager userNotificationManager)
    {
        _realTimeNotifier = realTimeNotifier;
        _userNotificationManager = userNotificationManager;
    }

    [UnitOfWork]
    public override void Execute(UserIdentifier args)
    {
        var notifications = _userNotificationManager.GetUserNotifications(args);
        AsyncHelper.RunSync(() => _realTimeNotifier.SendNotificationsAsync(notifications.ToArray()));
    }
}

客户端代码

function loadWorkOrderDirectories() {
  workOrderDirectoryService.getList().success(function(result) {
    $scope.workOrderDirectories = result.items;
    publishNotifications($scope.workOrderDirectories);
    showUInotifications();
  });
};

loadWorkOrderDirectories();

function publishNotifications(arr) {
  angular.forEach(arr, function(value, key) {
    workOrderDirectoryService.publishCreated(value.id, 2);
  });
};

function showUInotifications() {
  abp.event.on('abp.notifications.received', function(userNotification) {
    abp.notifications.showUiNotifyForUserNotification(userNotification);
    $scope.notis = userNotification;
    if (userNotification.notification.data.type === 'Abp.Notifications.LocalizableMessageNotificationData') {
      var localizedText = abp.localization.localize(
        userNotification.notification.data.message.name,
        userNotification.notification.data.message.sourceName
      );

      $.each(userNotification.notification.data.properties, function(key, value) {
        localizedText = localizedText.replace('{' + key + '}', value);
      });

      alert('New localized notification: ' + localizedText);
    } else if (userNotification.notification.data.type === 'Abp.Notifications.MessageNotificationData') {
      alert('New simple notification: ' + userNotification.notification.data.message);
    }
  });
};

我正在关注这篇文章:https://aspnetboilerplate.com/Pages/Articles/Developing-MultiTenant-SaaS-ASP.NET-CORE-Angular/index.html

_realTimeNotifier._onlineClientManager.Clients 为空。这是我得到的:https://ibb.co/0qmqFfN

请帮助我。提前谢谢你。

【问题讨论】:

  • 你没有使用免费的零模块模板吗?
  • 共享从该模板派生的复制项目。
  • 您能否通过在Execute 中设置断点来确认_realTimeNotifier._onlineClientManager.Clients[0].Value.UserIdnull

标签: c# asp.net-mvc notifications signalr aspnetboilerplate


【解决方案1】:
  1. layout.cshtml中为jquery.signalR-2.4.0.js添加一个脚本标签。

    <!--SIGNAL R-->
    <script src="~/Scripts/jquery.signalR-2.4.0.js"></script> <!-- Add this -->
    <script src="~/signalr/hubs"></script>
    <script src="~/Abp/Framework/scripts/libs/abp.signalr.js"></script>
    
  2. Startup.cs 中验证后,将app.MapSignalR(); 移动到Configuration 的末尾。

    public void Configuration(IAppBuilder app)
    {
        // app.MapSignalR(); // Move this...
    
        // ...
    
        app.MapSignalR(); // ...here!
    }
    

【讨论】:

    猜你喜欢
    • 2021-07-29
    • 2017-10-24
    • 2021-09-27
    • 2018-10-12
    • 2016-12-08
    • 2018-05-31
    • 2019-11-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多