简介

在ABP中,提供了通知服务。它是一个基于实时通知的基础设施。分为订阅模式和发布模式。

本次会在项目中使用发布模式来演示一个用户注册后,收到的欢迎信息。

发布模式

首先我们在领域层建立“INotificationManager”接口和“NotificationManager”实现类,如下:

 /// <summary>
/// 通知管理领域服务
/// </summary>
public interface INotificationManager
{
Task WelcomeToCmsAsync(User user);
}
   public class NotificationManager : CmsDomainServiceBase, INotificationManager
{
private readonly INotificationPublisher _notificationPublisher;
public NotificationManager(INotificationPublisher notificationPublisher)
{
_notificationPublisher = notificationPublisher;
}
public async Task WelcomeToCmsAsync(User user)
{
await _notificationPublisher.PublishAsync(
CmsConsts.NotificationConstNames.WelcomeToCms,
new MessageNotificationData(L("WelcomeToCms")),severity:NotificationSeverity.Success,userIds:new []{user.ToUserIdentifier()} } }

以上就是一个简单的欢迎信息。

以上我们来分析下,调用的"INotificationManager"中的PublishAsync方法。

在源代码的定义中,我们看到了他有8个参数,我们来说说他具体去干嘛了,首先在ABP源代码中调用了这个方法之后,他会到实体NotificationInfo和UserNotification两个实体中进行处理,源代码中的结构很清晰。关联关系很明确是一个一对多的关系。

这个也就是我们在调用PublishAsync方法的时候需要我们键入一个键名,他是全局唯一的,在ABP的代码中的限制也是说明了,禁止使用空格和空字符串。

实时通知

我们刚刚说到了在PublishAsync中,涉及到了UserNotification实体,他的作用是通知用户消息,具体的代码实现中涉及到了后台作业和实时通知SignalR,而我们在客户端获取信息的时候,ABP是触发了一个全局事件。

···

abp.event.on('abp.notifications.received', function (userNotification) {

console.log(userNotification);

});

···

此处涉及的知识点为领域事件中的事件总线。

打印userNotification的Json格式如下:

{
id: "9c0aaf34-c620-4d05-89df-1f4ae11f3686",
notification: {
creationTime: "2017-05-05T01:27:25.7639034Z"
data: {
message: "This is a test notification, created at 2017/5/5 1:27:25" type: "Abp.Notifications.MessageNotificationData"
}
entityId: null
entityType: null
entityTypeName: null
id: "724d670e-2cfa-4656-a9b6-ff76ceba5ce3"
notificationName: "App.SimpleMessage"
severity: 0
tenantId: 1
}
state: 0
tenantId: 1
userId: 2
}

在这个对象中:

  • userId:当前用户Id。通常你不需要知道这个,因为你知道那个是当前用户。

  • state:枚举类型 UserNotificationState 的值。 0:Unread,1:Read。

  • notification:通知详细信息:

  • notificationName: 通知的唯一名字(发布通知时使用相同的值)。

  • data:通知数据。在上面例子中,我们使用了 LocalizableMessageNotificationData (在之前的例子中我们使用它来发布的)。

    • message: 本地化信息。在UI端,我们可以使用 sourceName 和 name 来本地化信息。

    • type:通知数据类型。类型的全名称,包含名称空间。当处理这个通知数据的时候,我们可以检查这个类型。

    • properties:自定义属的基于字典类型的属性。

  • entityType,entityTypeName 和 entityId:实体信息,如果这是一个与实体相关的通知。

  • severity:枚举类型 NotificationSeverity 的值。0: Info, 1: Success, 2: Warn, 3: Error, 4: Fatal。

  • creationTime:表示通知被创建的时间。

  • id:通知的id。

  • id:用户通知id。

以上就是消息通知功能的一个介绍,我们来实现这么个功能吧。

YoYoCms中消息通知的应用

我们继续在NotificationManager中添加方法SendMessageAsync


public async Task SendMessageAsync(UserIdentifier user, string messager, NotificationSeverity severity = NotificationSeverity.Info)
{
await _notificationPublisher.PublishAsync(
CmsConsts.NotificationConstNames.SendMessageAsync,
new MessageNotificationData(messager),severity:severity,userIds:new []{user});
}

然后在Account控制器中进行调用:


[AbpMvcAuthorize]
public async Task<ActionResult> TestNotification( string message = "" , NotificationSeverity severity = NotificationSeverity.Info)
{
if (message.IsNullOrEmpty())
{
message = "这是一条测试消息 " + Clock.Now;
}
await _notificationManager.SendMessageAsync(AbpSession.ToUserIdentifier(), message, severity: severity); return Content("发送提示信息: " + message);
}



然后我们可以在TenantNotifications和UserNotifications可以看到发布的信息和关联关系。

登陆到管理中心可以看到:

当前的用户信息,通知。

   vm.unReadUserNotificationCount = 0;
vm.userNotifications = [];
//格式化消息
var formattedMessage = function (item) { var message = {
id: item.id,
text: abp.notifications.getFormattedMessageFromUserNotification(item),
time: item.notification.creationTime,
image: getNotificationImgBySeverity(item.notification.severity),
state: abp.notifications.getUserNotificationStateAsString(item.state),
} return message; }
//获取图片路径
function getNotificationImgBySeverity(severity) {
switch (severity) {
case abp.notifications.severity.SUCCESS:
return '/App/assets/yoyocms/notification/1.png';
case abp.notifications.severity.WARN:
return '/App/assets/yoyocms/notification/2.png';
case abp.notifications.severity.ERROR:
return '/App/assets/yoyocms/notification/3.png';
case abp.notifications.severity.FATAL:
return '/App/assets/yoyocms/notification/4.png';
case abp.notifications.severity.INFO:
default:
return '/App/assets/yoyocms/notification/0.png';
}
} //获取所有的消息信息
vm.getUserNotificationsAsync= function() {
notificationService.getPagedUserNotificationsAsync({
maxResultCount: 5
}).then(function(result) {
vm.unReadUserNotificationCount = result.data.unreadCount;
vm.userNotifications = [];
$.each(result.data.items,
function (index, item) {
vm.userNotifications.push(formattedMessage(item));
});
console.log(vm.userNotifications);
}); }
//标记所有为已读
vm.makeAllAsRead= function() {
notificationService.makeAllUserNotificationsAsRead().then(function() {
vm.getUserNotificationsAsync();
});
}
//设置消息为已读
vm.makeNotificationAsRead = function (userNotification) {
notificationService.makeNotificationAsRead({ id: userNotification.id }).
then(function () {
for (var i = 0; i < vm.userNotifications.length; i++) {
if (vm.userNotifications[i].id == userNotification.id) {
vm.userNotifications[i].state = 'READ';
}
}
vm.unReadUserNotificationCount -= 1; });
} //初始化方法
function init() {
vm.getUserNotificationsAsync(); } init(); //测试为领域事件的一个例子
abp.event.on('abp.notifications.received', function (userNotification) {
abp.notifications.showUiNotifyForUserNotification(userNotification);
vm.getUserNotificationsAsync();
});
}

其中有'abp.notifications.received'是一个领域事件的简单实现,大家有兴趣可以继续研究。以上整个功能已经发布到GitHub上,欢迎大家下载使用。

THE END

本开源项目的地址为:https://github.com/ltm0203/YoYoCms

预览网址为:http://www.yoyocms.com/

涉及的技术选型:https://github.com/ltm0203/YoYoCms/tree/dev/doc

[ABP实战开源项目]---ABP实时服务-通知系统.发布模式的更多相关文章

  1. [ABP实战开源项目]--YoYoCms目录

    ABP是"ASP.NET Boilerplate Project (ASP.NET样板项目)"的简称. ASP.NET Boilerplate是一个用最佳实践和流行技术开发现代WE ...

  2. 推荐一款稳定快速免费的前端开源项目 CDN 加速服务

    前面学习到什么是CDN,全称是Content Delivery Network,即内容分发网络.CDN的通俗理解就是网站加速,CPU均衡负载. CDN的基本思路是尽可能避开互联网上有可能影响数据传输速 ...

  3. 开源项目 AllJoyn 基础服务

    AllJoyn 基础服务主要包含 Onboarding,Notification 和 Control Panel三个大项. 这三个也是编写App的最基础的,最经常使用的部分. Onboarding 提 ...

  4. .NET ORM 开源项目 FreeSql 1.0 正式版发布

    一.简介 FreeSql 是 .NET 平台下的对象关系映射技术(O/RM),支持 .NetCore 2.1+ 或 .NetFramework 4.0+ 或 Xamarin. 从 0.0.1 发布,历 ...

  5. 开源项目收集:博客系统solo

    前言 一个好的项目,我不会吝啬于推荐之语.找了好久,想要一个最简单的个人博客.由于个人不怎么会写前端页面,怎么也看不到漂亮的设计.没有漂亮的前台都不知道后台需要写一些什么! 这个项目至少目前满足了我的 ...

  6. DIOCP开源项目-DIOCP3的重生和稳定版本发布

    DIOCP3的重生 从开始写DIOCP到现在已经有一年多的时间了,最近两个月以来一直有个想法做个 30 * 24 稳定的企业服务端架构,让程序员专注于逻辑实现就好.虽然DIOCP到现在通讯层已经很稳定 ...

  7. 优秀开源项目之一:视频监控系统iSpy

    iSpy是一个开源的视频监控软件,目前已经支持中文.自己用了一下,感觉还是很好用的.翻译了一下它的介绍. iSpy将PC变成一个完整的安全和监控系统 iSpy使用您的摄像头和麦克风来检测和记录声音或运 ...

  8. Halo 开源项目学习(四):发布文章与页面

    基本介绍 博客最基本的功能就是让作者能够自由发布自己的文章,分享自己观点,记录学习的过程.Halo 为用户提供了发布文章和展示自定义页面的功能,下面我们分析一下这些功能的实现过程. 管理员发布文章 H ...

  9. 实战小项目之RTMP流媒体演示系统

    项目简介 windows下使用基于Qt对之前的RtmpApp进行封装与应用,单独功能使用线程执行,主要包括以下几个功能: 视频下载 推送文件 推送摄像头数据或者桌面 基于libvlc的播放器 视频下载 ...

随机推荐

  1. 交叉编译Python-2.7.13到ARM(aarch32)平台

    作者:彭东林 邮箱:pengdonglin137@163.com QQ:405728433 环境 主机: ubuntu14.04 64bit 开发板: qemu + vexpress-a9 (参考: ...

  2. Windows搭建以太坊的私有链环境

    1.下载Geth.exe 运行文件,并安装 https://github.com/ethereum/go-ethereum/releases/ 下载后,只有一个Geth.exe的文件 2.cmd进入按 ...

  3. 让我的分页类获取sessionFactory

    我们知道在Hibernate里比较重要的sessionFactory,经过Spring的管理可以很好地为Spring里注入使用的bean服务(提供数据源的使用),但是,当我们所要使用的类不是像我们尝试 ...

  4. iOS开发之Autolayout

    1.概述 在以前的iOS程序中,是如何设置布局UI界面的? (1)经常编写大量的坐标计算代码 (2)为了保证在3.5 inch和4.0 inch屏幕上都能有完美的UI界面效果,有时还需要分别为2种屏幕 ...

  5. Unity3D中的AI架构模型

    我们都知道现在AI(由人工制造出来的系统所表现出来的模拟人类的智能活动)非常的火,可以说是家喻户晓.当然,在游戏中,AI也是到处可以找到的,对于AI,我们应该关注的问题是如何让游戏角色能够向人或动物那 ...

  6. Unity3d在Window上使用SAPI进行语音识别

    前言 在之前<Unity利用Sapi进行windows语音开发>中,本计划不准备继续做语音识别.因为在unity3d中已经提供了语音识别的相关方法,详见unity3d的官方文档:https ...

  7. 判断是否是IE(包含IE11)

    判断是否是IE(包含IE11) if(!!window["ActiveXObject"] || "ActiveXObject" in window) { ale ...

  8. WPF之路三:视频的播放

    同图片一样,把视频资源复制到文件夹路径下,修改资源属性复制到输出目录"始终复制",生成操作改为“内容”, 把<MediaElement  Name="myVid&q ...

  9. 在程序加载过程中显示ProgressDialog 对话框

    private ProgressDialog mProgressDlg = null; @Override protected void onCreate(Bundle savedInstanceSt ...

  10. 4月6日--js生成随机数列

    newarr=[1,2,3,4,5,6] function randomsort(a,b){ return Math.random()>0.5?-1:1;}//用Math.random()函数生 ...