我正在使用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);
}
});
};
_realTimeNotifier._onlineClientManager.Clients
是空的。这就是我得到的:https://ibb.co/0qmqFfN
请帮我。先感谢您。
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>
app.MapSignalR();
移动到Configuration
的末尾。
public void Configuration(IAppBuilder app)
{
// app.MapSignalR(); // Move this...
// ...
app.MapSignalR(); // ...here!
}