关于ABP通知功能的使用心得

1.如果想使用ABP的通知功能,首先需要在你的业务类中注入 INotificationPublisher 接口。

比如: private readonly INotificationPublisher _notiticationPublisher; ,然后再通过构造方法注入。它的作用就是发布通知。

2.注入 INotificationPublisher 之后,调用它的 Publish 方法,比如:

_notiticationPublisher.Publish("SentFrendshipRequest",
    new SentFrendshipRequestNotificationData("admin", "test_xxxxx"),
    userIds: new[] { new UserIdentifier(AbpSession.TenantId, AbpSession.UserId.To<long>()) }
);

它还有另外一个方法: PublishAsnyc 用于异步发布。

这两个方法的第2个参数是一个自定义的类,继承了NotificationData,你可以随便定义你想要的类,只要继承NotificationData就行了。

3.新建一个 RabbitMqNotifier 类,实现 IRealTimeNotifier 接口,比如:

    /// <summary>
    /// 
    /// </summary>
    public class RabbitMqNotifier : IRealTimeNotifier, ITransientDependency
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="userNotifications"></param>
        public void SendNotifications(UserNotification[] userNotifications)
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="userNotifications"></param>
        /// <returns></returns>
        public Task SendNotificationsAsync(UserNotification[] userNotifications)
        {
            throw new NotImplementedException();
        }
    }

3.1.如果发布时使用的同步方法,那么 SendNotifications 方法会起作用;如果发布时用的是异步方法,那么 SendNotificationsAsync 方法会起作用。
3.2.需要在你的Module类里面配置你定义的这个Notifier: Configuration.Notifications.Notifiers.Add<RabbitMqNotifier>(); 网上很多资料都没说明要加这个。
3.3. _notiticationPublisher 调用发布方法时,通过看其源码,里面有调用 IRealTimeNotifier 的实现类, IRealTimeNotifier 是通过构造方法注入的。

此处订阅省略。。。

特此记录下来,以防忘记。

posted @ 2022-03-17 23:03  屌丝大叔的笔记  阅读(264)  评论(0编辑  收藏  举报