Pabasara Mahindapala
Pabasara Mahindapala

Reputation: 322

Unable to send real-time notifications saved in database

I'm using the ASP.NET Boilerplate template and trying to get real-time entity-based notifications for specific users. My notifications are published successfully in the Abp.TenantNotifications table and the subscriptions are saved successfully in the Abp.NotificationSubscriptions table. But I can't get it to be displayed on the client side.

I have integrated my project with SignalR according to the documentation.


Application Service where I publish the notification:

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

Background Job:

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

Client-side code:

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


I'm following this article: https://aspnetboilerplate.com/Pages/Articles/Developing-MultiTenant-SaaS-ASP.NET-CORE-Angular/index.html

_realTimeNotifier._onlineClientManager.Clients is empty. Here's what I got: https://ibb.co/0qmqFfN

Please help me. Thank you in advance.

Upvotes: 2

Views: 1610

Answers (1)

aaron
aaron

Reputation: 43103

  1. Add a script tag for jquery.signalR-2.4.0.js in layout.cshtml.

    <!--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. Move app.MapSignalR(); to the end of Configuration, after authentication, in Startup.cs.

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

Upvotes: 1

Related Questions