NotificationManager.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. using System;
  2. using System.Threading.Tasks;
  3. using Abp;
  4. using Abp.Dependency;
  5. using Abp.Notifications;
  6. using Abp.Runtime.Session;
  7. using WeOnlineApp.Configuration;
  8. using IwbZero.Runtime.Session;
  9. namespace WeOnlineApp.CommonManager.Notifications
  10. {
  11. public class NotificationManager : INotificationManager, ISingletonDependency
  12. {
  13. private readonly INotificationPublisher _notificationPublisher;
  14. private readonly IwbSession _abpSession;
  15. public NotificationManager(INotificationPublisher notificationPublisher, IwbSession abpSession)
  16. {
  17. _notificationPublisher = notificationPublisher;
  18. _abpSession = abpSession;
  19. }
  20. public async Task WelcomeToSystemAsync()
  21. {
  22. await _notificationPublisher.PublishAsync(
  23. IwbNotificationName.WelcomeMsg, new MessageNotificationData("欢迎登陆"), severity: NotificationSeverity.Success,
  24. userIds: new[] { _abpSession.ToUserIdentifier() });
  25. }
  26. /// <summary>
  27. /// 发送给指定用户消息(info)
  28. /// </summary>
  29. /// <param name="user"></param>
  30. /// <param name="message"></param>
  31. /// <returns></returns>
  32. public async Task SendMsgAsync(UserIdentifier user, string message)
  33. {
  34. await SendMsgAsync(user, message, NotificationSeverity.Info);
  35. }
  36. /// <summary>
  37. /// 发送给指定用户消息
  38. /// </summary>
  39. /// <param name="user"></param>
  40. /// <param name="message"></param>
  41. /// <param name="severity"></param>
  42. /// <returns></returns>
  43. public async Task SendMsgAsync(UserIdentifier user, string message, NotificationSeverity severity)
  44. {
  45. await _notificationPublisher.PublishAsync(
  46. IwbNotificationName.SendMsg,
  47. new MessageNotificationData(message), severity: severity, userIds: new[] { user });
  48. }
  49. /// <summary>
  50. /// 发送某种消息给订阅用户(Info)
  51. /// </summary>
  52. /// <param name="msgName">消息唯一名称</param>
  53. /// <param name="message"></param>
  54. /// <returns></returns>
  55. public async Task SendMsgAsync(string msgName, string message)
  56. {
  57. await SendMsgAsync(msgName, message, NotificationSeverity.Info);
  58. }
  59. /// <summary>
  60. /// 发送某种消息给订阅用户
  61. /// </summary>
  62. /// <param name="msgName">消息唯一名称</param>
  63. /// <param name="message"></param>
  64. /// <param name="severity"></param>
  65. /// <returns></returns>
  66. public async Task SendMsgAsync(string msgName, string message, NotificationSeverity severity)
  67. {
  68. await _notificationPublisher.PublishAsync(msgName, new MessageNotificationData(message), severity: severity);
  69. }
  70. /// <summary>
  71. /// 发送某种消息给指定用户(带发送人)
  72. /// </summary>
  73. /// <param name="user"></param>
  74. /// <param name="msgName"></param>
  75. /// <param name="data"></param>
  76. /// <param name="severity"></param>
  77. /// <returns></returns>
  78. public async Task SendMessageAsync(UserIdentifier user, string msgName, SendMsgNotificationData data, NotificationSeverity severity)
  79. {
  80. await _notificationPublisher.PublishAsync(msgName, data, severity: severity, userIds: new[] { user });
  81. }
  82. /// <summary>
  83. /// 发送某种消息给订阅用户(带发送人)
  84. /// </summary>
  85. /// <param name="msgName"></param>
  86. /// <param name="data"></param>
  87. /// <param name="severity"></param>
  88. /// <returns></returns>
  89. public async Task SendMessageAsync(string msgName, SendMsgNotificationData data, NotificationSeverity severity)
  90. {
  91. await _notificationPublisher.PublishAsync(msgName, data, severity: severity);
  92. }
  93. }
  94. [Serializable]
  95. public class SendMsgNotificationData : NotificationData
  96. {
  97. public string SenderUserName { get; set; }
  98. public string Message { get; set; }
  99. public SendMsgNotificationData(string senderUserName, string message)
  100. {
  101. SenderUserName = senderUserName;
  102. Message = message;
  103. }
  104. }
  105. }