[ABP实战开源项目]---ABP实时服务-通知系统.发布模式
简介
在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实时服务-通知系统.发布模式的更多相关文章
- [ABP实战开源项目]--YoYoCms目录
ABP是"ASP.NET Boilerplate Project (ASP.NET样板项目)"的简称. ASP.NET Boilerplate是一个用最佳实践和流行技术开发现代WE ...
- 推荐一款稳定快速免费的前端开源项目 CDN 加速服务
前面学习到什么是CDN,全称是Content Delivery Network,即内容分发网络.CDN的通俗理解就是网站加速,CPU均衡负载. CDN的基本思路是尽可能避开互联网上有可能影响数据传输速 ...
- 开源项目 AllJoyn 基础服务
AllJoyn 基础服务主要包含 Onboarding,Notification 和 Control Panel三个大项. 这三个也是编写App的最基础的,最经常使用的部分. Onboarding 提 ...
- .NET ORM 开源项目 FreeSql 1.0 正式版发布
一.简介 FreeSql 是 .NET 平台下的对象关系映射技术(O/RM),支持 .NetCore 2.1+ 或 .NetFramework 4.0+ 或 Xamarin. 从 0.0.1 发布,历 ...
- 开源项目收集:博客系统solo
前言 一个好的项目,我不会吝啬于推荐之语.找了好久,想要一个最简单的个人博客.由于个人不怎么会写前端页面,怎么也看不到漂亮的设计.没有漂亮的前台都不知道后台需要写一些什么! 这个项目至少目前满足了我的 ...
- DIOCP开源项目-DIOCP3的重生和稳定版本发布
DIOCP3的重生 从开始写DIOCP到现在已经有一年多的时间了,最近两个月以来一直有个想法做个 30 * 24 稳定的企业服务端架构,让程序员专注于逻辑实现就好.虽然DIOCP到现在通讯层已经很稳定 ...
- 优秀开源项目之一:视频监控系统iSpy
iSpy是一个开源的视频监控软件,目前已经支持中文.自己用了一下,感觉还是很好用的.翻译了一下它的介绍. iSpy将PC变成一个完整的安全和监控系统 iSpy使用您的摄像头和麦克风来检测和记录声音或运 ...
- Halo 开源项目学习(四):发布文章与页面
基本介绍 博客最基本的功能就是让作者能够自由发布自己的文章,分享自己观点,记录学习的过程.Halo 为用户提供了发布文章和展示自定义页面的功能,下面我们分析一下这些功能的实现过程. 管理员发布文章 H ...
- 实战小项目之RTMP流媒体演示系统
项目简介 windows下使用基于Qt对之前的RtmpApp进行封装与应用,单独功能使用线程执行,主要包括以下几个功能: 视频下载 推送文件 推送摄像头数据或者桌面 基于libvlc的播放器 视频下载 ...
随机推荐
- SQL AlawaysOn 之五:ISCSI共享磁盘
用于存放SQL数据库 1.安装服务 2.安装完成后要求重启计算机.添加该功能要配置计算机,如果是正式服务器,那种不能关机太久的服务器,请慎用. 重启之后看到文件和储存服务,击击进去 3.看到ISCSI ...
- 对于反射中的invoke()方法的理解
先讲一下java中的反射: 反射就是将类别的各个组成部分进行剖析,可以得到每个组成部分,就可以对每一部分进行操作 在比较复杂的程序或框架中来使用反射技术,可以简化代码提高程序的复用性. 讲的是Meth ...
- Node.js开发工具、开发包、框架等总结
开发工具 1.WebStorm,毫无疑问非他莫属,跨平台,强大的代码提示,支持Nodejs调试,此外还支持vi编辑模式,这点我很喜欢.2.做些小型项目用Sublime Text.3.Browserif ...
- Linux之cut命令
cut 参数: -d 指定分隔符,与-f 一起使用,默认是空格.例如:-d“|” -f 指定取第几段的数据与-d一起使用 -c 以字符为单位取出固定字符区间 示例: 取不连续区间的内容的时候使用 ...
- OpenStack_Glance
什么是Glace Glance即image service(镜像服务),就是为创建虚拟机提供镜像的地方 为什么要有Glance 这个问题问的好,openstack就是构建Iaas平台对外提供虚拟机的啊 ...
- 搭建ntp 时钟服务器_Linux
一.搭建时间同步服务器1.编译安装ntp serverwget [url]http://www.eecis.udel.edu/~ntp/ntp_spool/ntp4/ntp-4.2.4p4.tar.g ...
- 6、CC2541修改按键调节广播发送功率例程为持续发送4DB的蓝牙基站
一.目的 在 OSAL操作系统-实验31 从机广播功率修改-(20141029更新).zip 基础上进行修改,该工程是通过5向按键的上下按键来控制广播功率的加减,总共有4个档位.我们的目的是直接用最高 ...
- 手机自动化培训:Appium介绍
手机自动化培训:Appium介绍 poptest是国内唯一一家培养测试开发工程师的培训机构,以学员能胜任自动化测试,性能测试,测试工具开发等工作为目标.如果对课程感兴趣,请大家咨询qq:9088214 ...
- 老李推荐:第8章1节《MonkeyRunner源码剖析》MonkeyRunner启动运行过程-运行环境初始化
老李推荐:第8章1节<MonkeyRunner源码剖析>MonkeyRunner启动运行过程-运行环境初始化 首先大家应该清楚的一点是,MonkeyRunner的运行是牵涉到主机端和目 ...
- Java 基础知识总结
作者QQ:1095737364 QQ群:123300273 欢迎加入! 1.数据类型: 数据类型:1>.基本数据类型:1).数值型: 1}.整型类型(byte 8位 (by ...