使用 SignalR 注册时的实时通知

Real-time notification upon registration using SignalR

我想在 ASP.NET 样板中发送实时通知。订阅中的通知已成功保存在 Abp.NotificationSubscription table 中。当我发布通知时,通知已保存在 Abp.Notification table 中,但不会实时显示给用户。

我的服务器端代码:

public async Task<RegisterOutput> Register(RegisterInput input)
{
    public async Task<RegisterOutput> Register(RegisterInput input)
    {
      var user = await _userRegistrationManager.RegisterAsync(
          input.Name,
          input.Surname,
          input.EmailAddress,
          input.UserName,
          input.Password,
          true
      );

      _notificationSubscriptionManager.SubscribeToAllAvailableNotifications(user.ToUserIdentifier());
      await _appNotifier.WelcomeToTheApplicationAsync(user);

      var notification = _userNotificationManager.GetUserNotifications(user.ToUserIdentifier());
      await _realTimeNotifier.SendNotificationsAsync(notification.ToArray());

      // ...
    }
}

_appNotifier 实施 WelcomeToTheApplicationAsync(user):

public async Task WelcomeToTheApplicationAsync(User user)
{
    await _notificationPublisher.PublishAsync(
        AppNotificationName.WelcomeToTheApplication,
        new SendNotificationData("Naeem", "Hello I have sended this notification to you"),
        severity: NotificationSeverity.Success,
        userIds: new[] { user.ToUserIdentifier() }
    );
}

SendNotificationData 继承自 NotificationData:

public class SendNotificationData : NotificationData
{
    public string name { get; set; }
    public string message { get; set; }

    public SendNotificationData(string _name, string _message)
    {
        name = _name;
        message = _message;
    }
}

I want to sent welcome notification to the user when he registers itself by using the register link on the login page. In the CloudbaseLine.Application project ... we have AccountAppService which contain the code for registering the user and sending notification to it after successful registration but the problem is notification go saved in Abp.NotificationSubscriptionTable and UserNotificationTable but user cannot received them.

public async Task<RegisterOutput> Register(RegisterInput input)
{
    var user = await _userRegistrationManager.RegisterAsync(
        input.Name,
        input.Surname,
        input.EmailAddress,
        input.UserName,
        input.Password,
        true
    );

    _notificationSubscriptionManager.SubscribeToAllAvailableNotifications(user.ToUserIdentifier());
    await _appNotifier.WelcomeToTheApplicationAsync(user);

    var notification = _userNotificationManager.GetUserNotifications(user.ToUserIdentifier());
    await _realTimeNotifier.SendNotificationsAsync(notification.ToArray());

    // ...
}
public async Task WelcomeToTheApplicationAsync(User user)
{
    await _notificationPublisher.PublishAsync(
        AppNotificationName.WelcomeToTheApplication,
        new SendNotificationData("Naeem", "Hello I have sended this notification to you"),
        severity: NotificationSeverity.Success,
        userIds: new[] { user.ToUserIdentifier() }
    );
}

用户未在 Register 方法中连接到 SignalR 集线器。

一种处理方法是 enqueue a background job:

await _backgroundJobManager.EnqueueAsync<WelcomeNotificationJob, UserIdentifier>(
    user.ToUserIdentifier(),
    delay: TimeSpan.FromSeconds(5)
);
public class WelcomeNotificationJob : BackgroundJob<UserIdentifier>, ITransientDependency
{
    private readonly IRealTimeNotifier _realTimeNotifier;
    private readonly IUserNotificationManager _userNotificationManager;

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

不要忘记 register data formatters 自定义通知数据类型,在客户端:

abp.notifications.messageFormatters['CloudBaseLine.Notification.SendNotificationData'] = function (userNotification) {
    return userNotification.notification.data.message;
}