c# - 使用 SignalR 注册后的实时通知

标签 c# asp.net-core notifications asp.net-boilerplate asp.net-core-signalr

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

我的服务器端代码:

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

关于c# - 使用 SignalR 注册后的实时通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50348627/

相关文章:

c# - 单个 LINQ to SQL 查询中的 SUM 和 COUNT

c# - 使用 GuidAttribute 动态声明的类

c# - 我是否遵循这种结构的 SRP?

c# - 将文件添加到 Flurl 多部分 POST 请求时 IFormFileCollection 为空

java - Android 警报管理器不会触发我的事件

android - 来自通知 : AlertDialog without UI

c# - 正则表达式在 C# 中不匹配

asp.net-core - 启动 dotnet 失败 ("dotnet"不是符号链接(symbolic link)的文件)

c# - 在服务器上发布我的 Core 2.2 项目后 web.config 无效

android - 如何在通知中添加声音?