未发送实时通知

Realtime notification not sent

我正尝试在 ASP.NET 样板中显示实时通知,但未发送。

public override async Task<MessagesDto> Create(MessagesCreateDto input)
{
    var MessagesCore = ObjectMapper.Map<MessagesCore>(input);            
    MessagesCore.UserId = Convert.ToInt32(AbpSession.UserId);
    MessagesCore.CreationId = Convert.ToInt32(AbpSession.UserId);
    MessagesCore.FromUserId = Convert.ToInt32(AbpSession.UserId);
    MessagesCore.CreationTime = DateTime.Now;
    MessagesCore.LastModificationId = Convert.ToInt32(AbpSession.UserId);
    MessagesCore.LastModificationTime = DateTime.Now;

    _stateRepository.Insert(MessagesCore);
    CurrentUnitOfWork.SaveChanges();

    UserIdentifier identifier = new UserIdentifier(1,input.ToUserId);
    await _notificationSubscriptionManager.SubscribeAsync(new UserIdentifier(1, input.ToUserId), "NewMessage");
    await _notificationPublisher.PublishAsync("NewMessage", new SentMessageNotificationData("Test", "New Message"), userIds: new[] { identifier });

    return MapToEntityDto(MessagesCore);
}

SentMessage通知数据class:

[Serializable]
public class SentMessageNotificationData : NotificationData
{
    public string SenderUserName { get; set; }

    public string FriendshipMessage { get; set; }

    public SentMessageNotificationData(string senderUserName, string friendshipMessage)
    {
        SenderUserName = senderUserName;
        FriendshipMessage = friendshipMessage;
    }
}

数据存储在通知中table,但客户端没有显示通知消息。

app.component.ts中的 SignalR 代码:

ngOnInit(): void {
  if (this.appSession.application.features['SignalR']) {
    SignalRHelper.initSignalR();
  }

  abp.event.on('abp.notifications.received', userNotification => {
    abp.notifications.showUiNotifyForUserNotification(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);
    }
    //Desktop notification
    Push.create("AbpZeroTemplate", {
      body: userNotification.notification.data.message,
      icon: abp.appPath + 'assets/app-logo-small.png',
      timeout: 6000,
      onClick: function () {
        window.focus();
        this.close();
      }
    });
  });
}

您是否在客户端创建了一个事件来处理通知消息。您应该像这样设置 abp.notifications.received 事件;

abp.event.on('abp.notifications.received', function (userNotification) {
    console.log(userNotification);
});

你可以找到更详细的解释here.

[更新]

您需要集成 SignalR 才能获得实时通知功能。按照文件 here

您的代码似乎是正确的。如果您在数据库中看到该记录,则 BackgroundJob 可能不会 运行 处理该记录。将调试器放在后台作业上以查看它是否遍历记录。

顺便说一下,我看到您传递的是 1 作为租户 ID。因此,如果您希望从租户而不是默认租户那里获得通知,它将永远不会送达。

UserIdentifier identifier = new UserIdentifier(1,input.ToUserId);

您是否在您的项目中添加了 Hub Class? 您需要在那里添加一个方法。 SignalR 主要通过 Hub 进行通信,并通过 Hub(服务器)从 Client 发送实时更新,然后从那里发送到目的地,即要显示实时更新的地方根据您的代码。

i am on netcoreapp2.0

  • 对于 .NET Core,SignalR 仍在 1.0.0-alpha2-final
  • 2.x 只会在 Q1/Q2 2018 年稳定。

更新 1

Abp.AspNetCore.SignalR NuGet 包已发布。

阅读 SignalR AspNetCore Integration 的文档。

您可以 try it and make the necessary changes 到您的文件。 或者等待发布。

更新 2

您现在可以 download v3.4.1 带有 AspNetCore.SignalR 预览的模板。