我想在 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/