【问题标题】:Realtime notification not sent未发送实时通知
【发布时间】:2018-05-27 11:25:41
【问题描述】:

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

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通知数据类:

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

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

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

【问题讨论】:

    标签: angular .net-core signalr aspnetboilerplate asp.net-core-signalr


    【解决方案1】:

    我在 netcoreapp2.0

    • SignalR 仍在 1.0.0-alpha2-final 上用于 .NET Core。
    • 2.x 将仅在 2018 年第一季度/第二季度稳定。

    更新 1

    Abp.AspNetCore.SignalR NuGet 包已发布。

    阅读SignalR AspNetCore Integration 的文档。

    您可以try 并为您的文件添加必要的changes或者只是等待它发布。

    更新 2

    您现在可以downloadv3.4.1 使用 AspNetCore.SignalR 预览模板。

    【讨论】:

      【解决方案2】:

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

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

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

      [更新]

      您需要集成 SignalR 才能获得实时通知。关注文档here

      【讨论】:

        【解决方案3】:

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

        【讨论】:

          【解决方案4】:

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

          顺便说一句,我看到您将 1 作为租户 ID 传递。因此,如果您希望收到来自租户而不是默认租户的通知,则永远不会收到通知。

          UserIdentifier 标识符 = new UserIdentifier(1,input.ToUserId);

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2019-10-13
            • 1970-01-01
            • 1970-01-01
            • 2021-12-06
            • 2010-10-18
            • 1970-01-01
            • 1970-01-01
            • 2015-12-29
            相关资源
            最近更新 更多