Browse Source

fix: Fixed the issue where notification messages might fail to serialize

pull/1328/head
colin 6 months ago
parent
commit
39f849c587
  1. 3
      aspnet-core/framework/common/LINGYUN.Abp.RealTime/LINGYUN/Abp/RealTime/Localization/LocalizableStringInfo.cs
  2. 4
      aspnet-core/modules/localization-management/LINGYUN.Abp.LocalizationManagement.Application/LINGYUN/Abp/LocalizationManagement/TextAppService.cs
  3. 21
      aspnet-core/modules/realtime-notifications/LINGYUN.Abp.Notifications.Core/LINGYUN/Abp/Notifications/NotificationStandardData.cs
  4. 84
      aspnet-core/modules/realtime-notifications/LINGYUN.Abp.Notifications.Emailing/LINGYUN/Abp/Notifications/Emailing/EmailingNotificationPublishProvider.cs
  5. 91
      aspnet-core/modules/realtime-notifications/LINGYUN.Abp.Notifications.PushPlus/LINGYUN/Abp/Notifications/PushPlus/PushPlusNotificationPublishProvider.cs
  6. 17
      aspnet-core/modules/realtime-notifications/LINGYUN.Abp.Notifications.Sms/LINGYUN/Abp/Notifications/Sms/SmsNotificationPublishProvider.cs
  7. 2
      aspnet-core/modules/realtime-notifications/LINGYUN.Abp.Notifications.Templating/LINGYUN.Abp.Notifications.Templating.csproj
  8. 21
      aspnet-core/modules/realtime-notifications/LINGYUN.Abp.Notifications.WeChat.MiniProgram/LINGYUN/Abp/Notifications/WeChat/MiniProgram/WeChatMiniProgramNotificationPublishProvider.cs
  9. 79
      aspnet-core/modules/realtime-notifications/LINGYUN.Abp.Notifications.WeChat.Work/LINGYUN/Abp/Notifications/WeChat/Work/WeChatWorkNotificationPublishProvider.cs
  10. 93
      aspnet-core/modules/realtime-notifications/LINGYUN.Abp.Notifications.WxPusher/LINGYUN/Abp/Notifications/WxPusher/WxPusherNotificationPublishProvider.cs
  11. 72
      aspnet-core/modules/realtime-notifications/LINGYUN.Abp.Notifications/LINGYUN/Abp/Notifications/DefaultNotificationDataSerializer.cs
  12. 5
      aspnet-core/modules/realtime-notifications/LINGYUN.Abp.Notifications/LINGYUN/Abp/Notifications/INotificationDataSerializer.cs

3
aspnet-core/framework/common/LINGYUN.Abp.RealTime/LINGYUN/Abp/RealTime/Localization/LocalizableStringInfo.cs

@ -24,6 +24,7 @@ public class LocalizableStringInfo
/// </summary>
public LocalizableStringInfo()
{
Values = new Dictionary<object, object>();
}
/// <summary>
/// Instantiate <see cref="LocalizableStringInfo"/>
@ -38,6 +39,6 @@ public class LocalizableStringInfo
{
ResourceName = resourceName;
Name = name;
Values = values;
Values = values ?? new Dictionary<object, object>();
}
}

4
aspnet-core/modules/localization-management/LINGYUN.Abp.LocalizationManagement.Application/LINGYUN/Abp/LocalizationManagement/TextAppService.cs

@ -1,13 +1,13 @@
using LINGYUN.Abp.LocalizationManagement.Features;
using LINGYUN.Abp.LocalizationManagement.Permissions;
using Microsoft.AspNetCore.Authorization;
using System.Runtime.Versioning;
using System.Threading.Tasks;
using Volo.Abp.Features;
namespace LINGYUN.Abp.LocalizationManagement;
[Authorize(LocalizationManagementPermissions.Text.Default)]
[RequiresPreviewFeatures(LocalizationManagementFeatures.Enable)]
[RequiresFeature(LocalizationManagementFeatures.Enable)]
public class TextAppService : LocalizationAppServiceBase, ITextAppService
{
private readonly ITextRepository _textRepository;

21
aspnet-core/modules/realtime-notifications/LINGYUN.Abp.Notifications.Core/LINGYUN/Abp/Notifications/NotificationStandardData.cs

@ -0,0 +1,21 @@
namespace LINGYUN.Abp.Notifications;
public class NotificationStandardData
{
public string Title { get; set; }
public string Message { get; set; }
public string Description { get; set; }
public NotificationStandardData()
{
}
public NotificationStandardData(
string title,
string message,
string description = null)
{
Title = title;
Message = message;
Description = description;
}
}

84
aspnet-core/modules/realtime-notifications/LINGYUN.Abp.Notifications.Emailing/LINGYUN/Abp/Notifications/Emailing/EmailingNotificationPublishProvider.cs

@ -1,45 +1,23 @@
using LINGYUN.Abp.Identity;
using LINGYUN.Abp.RealTime.Localization;
using Markdig;
using Microsoft.Extensions.Localization;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Volo.Abp.Emailing;
using Volo.Abp.Localization;
using Volo.Abp.Features;
namespace LINGYUN.Abp.Notifications.Emailing;
public class EmailingNotificationPublishProvider : NotificationPublishProvider
{
public const string ProviderName = NotificationProviderNames.Emailing;
public override string Name => ProviderName;
protected AbpLocalizationOptions LocalizationOptions { get; }
protected IEmailSender EmailSender { get; }
protected IStringLocalizerFactory LocalizerFactory { get; }
protected IIdentityUserRepository UserRepository { get; }
public EmailingNotificationPublishProvider(
IEmailSender emailSender,
IStringLocalizerFactory localizerFactory,
IIdentityUserRepository userRepository,
IOptions<AbpLocalizationOptions> localizationOptions)
{
EmailSender = emailSender;
LocalizerFactory = localizerFactory;
UserRepository = userRepository;
LocalizationOptions = localizationOptions.Value;
}
protected IEmailSender EmailSender => ServiceProvider.LazyGetRequiredService<IEmailSender>();
protected IFeatureChecker FeatureChecker => ServiceProvider.LazyGetRequiredService<IFeatureChecker>();
protected IIdentityUserRepository UserRepository => ServiceProvider.LazyGetRequiredService<IIdentityUserRepository>();
protected INotificationDataSerializer NotificationDataSerializer => ServiceProvider.LazyGetRequiredService<INotificationDataSerializer>();
protected async override Task PublishAsync(
NotificationInfo notification,
@ -55,56 +33,8 @@ public class EmailingNotificationPublishProvider : NotificationPublishProvider
Logger.LogWarning("The subscriber did not confirm the email address and could not send email notifications!");
return;
}
var notificationData = await NotificationDataSerializer.ToStandard(notification.Data);
if (!notification.Data.NeedLocalizer())
{
var title = notification.Data.TryGetData("title").ToString();
var message = notification.Data.TryGetData("message").ToString();
// markdown进行处理
if (notification.ContentType == NotificationContentType.Markdown)
{
message = Markdown.ToHtml(message);
}
await EmailSender.SendAsync(emailAddress, title, message);
}
else
{
var titleInfo = notification.Data.TryGetData("title").As<LocalizableStringInfo>();
var titleLocalizer = await LocalizerFactory.CreateByResourceNameAsync(titleInfo.ResourceName);
var title = titleLocalizer[titleInfo.Name].Value;
if (titleInfo.Values != null)
{
foreach (var formatTitle in titleInfo.Values)
{
if (formatTitle.Key != null && formatTitle.Value != null)
{
title = title.Replace($"{{{formatTitle.Key}}}", formatTitle.Value.ToString());
}
}
}
var messageInfo = notification.Data.TryGetData("message").As<LocalizableStringInfo>();
var messageLocalizer = await LocalizerFactory.CreateByResourceNameAsync(messageInfo.ResourceName);
var message = messageLocalizer[messageInfo.Name].Value;
if (messageInfo.Values != null)
{
foreach (var formatMessage in messageInfo.Values)
{
if (formatMessage.Key != null && formatMessage.Value != null)
{
message = message.Replace($"{{{formatMessage.Key}}}", formatMessage.Value.ToString());
}
}
}
// markdown进行处理
if (notification.ContentType == NotificationContentType.Markdown)
{
message = Markdown.ToHtml(message);
}
await EmailSender.SendAsync(emailAddress, title, message);
}
await EmailSender.SendAsync(emailAddress, notificationData.Title, notificationData.Message);
}
}

91
aspnet-core/modules/realtime-notifications/LINGYUN.Abp.Notifications.PushPlus/LINGYUN/Abp/Notifications/PushPlus/PushPlusNotificationPublishProvider.cs

@ -1,49 +1,25 @@
using LINGYUN.Abp.PushPlus.Channel;
using LINGYUN.Abp.PushPlus.Features;
using LINGYUN.Abp.PushPlus.Message;
using LINGYUN.Abp.RealTime.Localization;
using Microsoft.Extensions.Localization;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Volo.Abp.Features;
using Volo.Abp.Localization;
using Volo.Abp.Settings;
namespace LINGYUN.Abp.Notifications.PushPlus;
public class PushPlusNotificationPublishProvider : NotificationPublishProvider
{
public const string ProviderName = "PushPlus";
public override string Name => ProviderName;
protected IFeatureChecker FeatureChecker { get; }
protected IPushPlusMessageSender PushPlusMessageSender { get; }
protected IStringLocalizerFactory LocalizerFactory { get; }
protected AbpLocalizationOptions LocalizationOptions { get; }
protected INotificationDefinitionManager NotificationDefinitionManager { get; }
public PushPlusNotificationPublishProvider(
IFeatureChecker featureChecker,
IPushPlusMessageSender pushPlusMessageSender,
IStringLocalizerFactory localizerFactory,
IOptions<AbpLocalizationOptions> localizationOptions,
INotificationDefinitionManager notificationDefinitionManager)
{
FeatureChecker = featureChecker;
PushPlusMessageSender = pushPlusMessageSender;
LocalizerFactory = localizerFactory;
LocalizationOptions = localizationOptions.Value;
NotificationDefinitionManager = notificationDefinitionManager;
}
protected IFeatureChecker FeatureChecker => ServiceProvider.LazyGetRequiredService<IFeatureChecker>();
protected ISettingProvider SettingProvider => ServiceProvider.LazyGetRequiredService<ISettingProvider>();
protected IPushPlusMessageSender PushPlusMessageSender => ServiceProvider.LazyGetRequiredService<IPushPlusMessageSender>();
protected INotificationDataSerializer NotificationDataSerializer => ServiceProvider.LazyGetRequiredService<INotificationDataSerializer>();
protected INotificationDefinitionManager NotificationDefinitionManager => ServiceProvider.LazyGetRequiredService<INotificationDefinitionManager>();
protected async override Task<bool> CanPublishAsync(NotificationInfo notification, CancellationToken cancellationToken = default)
{
@ -77,49 +53,16 @@ public class PushPlusNotificationPublishProvider : NotificationPublishProvider
?? PushPlusMessageTemplate.Text;
var webhook = notification.Data.GetWebhookOrNull() ?? "";
var callbackUrl = notification.Data.GetCallbackUrlOrNull() ?? "";
if (!notification.Data.NeedLocalizer())
{
var title = notification.Data.TryGetData("title").ToString();
var message = notification.Data.TryGetData("message").ToString();
await PushPlusMessageSender.SendWithChannelAsync(
title,
message,
topic,
channelType: channel,
template: template,
webhook: webhook,
callbackUrl: callbackUrl,
cancellationToken: cancellationToken);
}
else
{
var titleInfo = notification.Data.TryGetData("title").As<LocalizableStringInfo>();
var titleResource = GetResource(titleInfo.ResourceName);
var titleLocalizer = await LocalizerFactory.CreateByResourceNameAsync(titleResource.ResourceName);
var title = titleLocalizer[titleInfo.Name, titleInfo.Values].Value;
var messageInfo = notification.Data.TryGetData("message").As<LocalizableStringInfo>();
var messageResource = GetResource(messageInfo.ResourceName);
var messageLocalizer = await LocalizerFactory.CreateByResourceNameAsync(messageResource.ResourceName);
var message = messageLocalizer[messageInfo.Name, messageInfo.Values].Value;
await PushPlusMessageSender.SendWithChannelAsync(
title,
message,
topic,
channelType: channel,
template: template,
webhook: webhook,
callbackUrl: callbackUrl,
cancellationToken: cancellationToken);
}
}
private LocalizationResourceBase GetResource(string resourceName)
{
return LocalizationOptions.Resources.Values
.First(x => x.ResourceName.Equals(resourceName));
var notificationData = await NotificationDataSerializer.ToStandard(notification.Data);
await PushPlusMessageSender.SendWithChannelAsync(
notificationData.Title,
notificationData.Message,
topic,
channelType: channel,
template: template,
webhook: webhook,
callbackUrl: callbackUrl,
cancellationToken: cancellationToken);
}
}

17
aspnet-core/modules/realtime-notifications/LINGYUN.Abp.Notifications.Sms/LINGYUN/Abp/Notifications/Sms/SmsNotificationPublishProvider.cs

@ -9,21 +9,10 @@ namespace LINGYUN.Abp.Notifications.Sms;
public class SmsNotificationPublishProvider : NotificationPublishProvider
{
public const string ProviderName = NotificationProviderNames.Sms;
protected IUserPhoneFinder UserPhoneFinder => ServiceProvider.LazyGetRequiredService<IUserPhoneFinder>();
protected ISmsNotificationSender Sender { get; }
protected AbpNotificationsSmsOptions Options { get; }
public SmsNotificationPublishProvider(
ISmsNotificationSender sender,
IOptions<AbpNotificationsSmsOptions> options)
{
Sender = sender;
Options = options.Value;
}
public override string Name => ProviderName;
protected IUserPhoneFinder UserPhoneFinder => ServiceProvider.LazyGetRequiredService<IUserPhoneFinder>();
protected ISmsNotificationSender Sender => ServiceProvider.LazyGetRequiredService<ISmsNotificationSender>();
protected IOptions<AbpNotificationsSmsOptions> Options => ServiceProvider.LazyGetRequiredService<IOptions<AbpNotificationsSmsOptions>>();
protected override async Task PublishAsync(
NotificationInfo notification,

2
aspnet-core/modules/realtime-notifications/LINGYUN.Abp.Notifications.Templating/LINGYUN.Abp.Notifications.Templating.csproj

@ -4,7 +4,7 @@
<Import Project="..\..\..\..\common.props" />
<PropertyGroup>
<TargetFrameworks>netstandard2.0;netstandard2.1;net8.0</TargetFrameworks>
<TargetFrameworks>netstandard2.0;netstandard2.1;net8.0;net9.0</TargetFrameworks>
<AssemblyName>LINGYUN.Abp.Notifications.Templating</AssemblyName>
<PackageId>LINGYUN.Abp.Notifications.Templating</PackageId>
<GenerateAssemblyConfigurationAttribute>false</GenerateAssemblyConfigurationAttribute>

21
aspnet-core/modules/realtime-notifications/LINGYUN.Abp.Notifications.WeChat.MiniProgram/LINGYUN/Abp/Notifications/WeChat/MiniProgram/WeChatMiniProgramNotificationPublishProvider.cs

@ -18,18 +18,9 @@ public class WeChatMiniProgramNotificationPublishProvider : NotificationPublishP
{
public const string ProviderName = NotificationProviderNames.WechatMiniProgram;
public override string Name => ProviderName;
protected IFeatureChecker FeatureChecker { get; }
protected ISubscribeMessager SubscribeMessager { get; }
protected AbpNotificationsWeChatMiniProgramOptions Options { get; }
public WeChatMiniProgramNotificationPublishProvider(
IFeatureChecker featureChecker,
ISubscribeMessager subscribeMessager,
IOptions<AbpNotificationsWeChatMiniProgramOptions> options)
{
Options = options.Value;
FeatureChecker = featureChecker;
SubscribeMessager = subscribeMessager;
}
protected IFeatureChecker FeatureChecker => ServiceProvider.LazyGetRequiredService<IFeatureChecker>();
protected ISubscribeMessager SubscribeMessager => ServiceProvider.LazyGetRequiredService<ISubscribeMessager>();
protected IOptions<AbpNotificationsWeChatMiniProgramOptions> Options => ServiceProvider.LazyGetRequiredService<IOptions<AbpNotificationsWeChatMiniProgramOptions>>();
protected async override Task<bool> CanPublishAsync(NotificationInfo notification, CancellationToken cancellationToken = default)
{
@ -75,10 +66,10 @@ public class WeChatMiniProgramNotificationPublishProvider : NotificationPublishP
var redirect = GetOrDefault(notification.Data, "RedirectPage", null);
Logger.LogDebug($"Get wechat weapp redirect page: {redirect ?? "null"}");
var weAppState = GetOrDefault(notification.Data, "WeAppState", Options.DefaultState);
var weAppState = GetOrDefault(notification.Data, "WeAppState", Options.Value.DefaultState);
Logger.LogDebug($"Get wechat weapp state: {weAppState ?? null}");
var weAppLang = GetOrDefault(notification.Data, "WeAppLanguage", Options.DefaultLanguage);
var weAppLang = GetOrDefault(notification.Data, "WeAppLanguage", Options.Value.DefaultLanguage);
Logger.LogDebug($"Get wechat weapp language: {weAppLang ?? null}");
// TODO: 如果微信端发布通知,请组装好 openid 字段在通知数据内容里面
@ -107,7 +98,7 @@ public class WeChatMiniProgramNotificationPublishProvider : NotificationPublishP
protected string GetOrDefaultTemplateId(NotificationData data)
{
return GetOrDefault(data, "TemplateId", Options.DefaultTemplateId);
return GetOrDefault(data, "TemplateId", Options.Value.DefaultTemplateId);
}
protected string GetOrDefault(NotificationData data, string key, string defaultValue)

79
aspnet-core/modules/realtime-notifications/LINGYUN.Abp.Notifications.WeChat.Work/LINGYUN/Abp/Notifications/WeChat/Work/WeChatWorkNotificationPublishProvider.cs

@ -1,10 +1,8 @@
using LINGYUN.Abp.RealTime.Localization;
using LINGYUN.Abp.WeChat.Work.Authorize;
using LINGYUN.Abp.WeChat.Work.Authorize;
using LINGYUN.Abp.WeChat.Work.Features;
using LINGYUN.Abp.WeChat.Work.Messages;
using LINGYUN.Abp.WeChat.Work.Messages.Models;
using LINGYUN.Abp.WeChat.Work.Settings;
using Microsoft.Extensions.Localization;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
@ -22,8 +20,8 @@ public class WeChatWorkNotificationPublishProvider : NotificationPublishProvider
public override string Name => ProviderName;
protected IFeatureChecker FeatureChecker => ServiceProvider.LazyGetRequiredService<IFeatureChecker>();
protected ISettingProvider SettingProvider => ServiceProvider.LazyGetRequiredService<ISettingProvider>();
protected IStringLocalizerFactory LocalizerFactory => ServiceProvider.LazyGetRequiredService<IStringLocalizerFactory>();
protected IWeChatWorkMessageSender WeChatWorkMessageSender => ServiceProvider.LazyGetRequiredService<IWeChatWorkMessageSender>();
protected INotificationDataSerializer NotificationDataSerializer => ServiceProvider.LazyGetRequiredService<INotificationDataSerializer>();
protected IWeChatWorkUserClaimProvider WeChatWorkInternalUserFinder => ServiceProvider.LazyGetRequiredService<IWeChatWorkUserClaimProvider>();
protected INotificationDefinitionManager NotificationDefinitionManager => ServiceProvider.LazyGetRequiredService<INotificationDefinitionManager>();
@ -55,65 +53,63 @@ public class WeChatWorkNotificationPublishProvider : NotificationPublishProvider
Logger.LogWarning("Unable to send work weixin messages because agentId is not set.");
return;
}
var title = "";
var message = "";
var description = "";
var notificationData = await NotificationDataSerializer.ToStandard(notification.Data);
var toTag = notification.Data.GetTagOrNull() ?? notificationDefine?.GetTagOrNull();
var toParty = notification.Data.GetPartyOrNull() ?? notificationDefine?.GetPartyOrNull();
if (!notification.Data.NeedLocalizer())
if (!toTag.IsNullOrWhiteSpace())
{
title = notification.Data.TryGetData("title").ToString();
message = notification.Data.TryGetData("message").ToString();
description = notification.Data.TryGetData("description")?.ToString() ?? "";
// 指定发送标签
await PublishToAgentAsync(
agentId,
notification,
notificationData.Title,
notificationData.Message,
notificationData.Description,
toTag: toTag,
cancellationToken: cancellationToken);
return;
}
else
var toParty = notification.Data.GetPartyOrNull() ?? notificationDefine?.GetPartyOrNull();
if (!toParty.IsNullOrWhiteSpace())
{
var titleInfo = notification.Data.TryGetData("title").As<LocalizableStringInfo>();
var titleLocalizer = await LocalizerFactory.CreateByResourceNameAsync(titleInfo.ResourceName);
title = titleLocalizer[titleInfo.Name, titleInfo.Values].Value;
var messageInfo = notification.Data.TryGetData("message").As<LocalizableStringInfo>();
var messageLocalizer = await LocalizerFactory.CreateByResourceNameAsync(messageInfo.ResourceName);
message = messageLocalizer[messageInfo.Name, messageInfo.Values].Value;
var descriptionInfo = notification.Data.TryGetData("description")?.As<LocalizableStringInfo>();
if (descriptionInfo != null)
{
var descriptionLocalizer = await LocalizerFactory.CreateByResourceNameAsync(descriptionInfo.ResourceName);
description = descriptionLocalizer[descriptionInfo.Name, descriptionInfo.Values].Value;
}
// 指定发送部门
await PublishToAgentAsync(
agentId,
notification,
notificationData.Title,
notificationData.Message,
notificationData.Description,
toParty: toParty,
cancellationToken: cancellationToken);
return;
}
var findUserList = await WeChatWorkInternalUserFinder
.FindUserIdentifierListAsync(identifiers.Select(id => id.UserId));
if (!findUserList.Any())
if (findUserList.Count == 0)
{
Logger.LogWarning("Unable to send work weixin messages because findUserList is empty.");
return;
}
// 发送到个人
await PublishToAgentAsync(
agentId,
notification,
findUserList.JoinAsString("|"),
title,
message,
description,
toParty,
toTag,
cancellationToken);
notificationData.Title,
notificationData.Message,
notificationData.Description,
toUser: findUserList.JoinAsString("|"),
cancellationToken: cancellationToken);
}
protected async virtual Task PublishToAgentAsync(
string agentId,
NotificationInfo notification,
string toUser,
string title,
string content,
string description = "",
string toUser = null,
string toParty = null,
string toTag = null,
CancellationToken cancellationToken = default)
@ -141,6 +137,13 @@ public class WeChatWorkNotificationPublishProvider : NotificationPublishProvider
return;
}
if (toUser.IsNullOrWhiteSpace() && toTag.IsNullOrWhiteSpace() && toParty.IsNullOrWhiteSpace())
{
// touser、toparty、totag不能同时为空:https://developer.work.weixin.qq.com/document/path/90236
Logger.LogWarning("Unable to send work weixin messages because The recipient/department/label cannot be empty simultaneously.");
return;
}
message.ToUser = toUser;
message.ToTag = toTag;
message.ToParty = toParty;

93
aspnet-core/modules/realtime-notifications/LINGYUN.Abp.Notifications.WxPusher/LINGYUN/Abp/Notifications/WxPusher/WxPusherNotificationPublishProvider.cs

@ -1,53 +1,24 @@
using LINGYUN.Abp.RealTime.Localization;
using LINGYUN.Abp.WxPusher.Features;
using LINGYUN.Abp.WxPusher.Features;
using LINGYUN.Abp.WxPusher.Messages;
using LINGYUN.Abp.WxPusher.User;
using Microsoft.Extensions.Localization;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Volo.Abp.Features;
using Volo.Abp.Localization;
namespace LINGYUN.Abp.Notifications.WxPusher;
public class WxPusherNotificationPublishProvider : NotificationPublishProvider
{
public const string ProviderName = "WxPusher";
public override string Name => ProviderName;
protected IFeatureChecker FeatureChecker { get; }
protected IWxPusherUserStore WxPusherUserStore { get; }
protected IWxPusherMessageSender WxPusherMessageSender { get; }
protected IStringLocalizerFactory LocalizerFactory { get; }
protected AbpLocalizationOptions LocalizationOptions { get; }
protected INotificationDefinitionManager NotificationDefinitionManager { get; }
public WxPusherNotificationPublishProvider(
IFeatureChecker featureChecker,
IWxPusherUserStore wxPusherUserStore,
IWxPusherMessageSender wxPusherMessageSender,
IStringLocalizerFactory localizerFactory,
IOptions<AbpLocalizationOptions> localizationOptions,
INotificationDefinitionManager notificationDefinitionManager)
{
FeatureChecker = featureChecker;
WxPusherUserStore = wxPusherUserStore;
WxPusherMessageSender = wxPusherMessageSender;
LocalizerFactory = localizerFactory;
LocalizationOptions = localizationOptions.Value;
NotificationDefinitionManager = notificationDefinitionManager;
}
protected IFeatureChecker FeatureChecker => ServiceProvider.LazyGetRequiredService<IFeatureChecker>();
protected IWxPusherUserStore WxPusherUserStore => ServiceProvider.LazyGetRequiredService<IWxPusherUserStore>();
protected IWxPusherMessageSender WxPusherMessageSender => ServiceProvider.LazyGetRequiredService<IWxPusherMessageSender>();
protected INotificationDataSerializer NotificationDataSerializer => ServiceProvider.LazyGetRequiredService<INotificationDataSerializer>();
protected INotificationDefinitionManager NotificationDefinitionManager => ServiceProvider.LazyGetRequiredService<INotificationDefinitionManager>();
protected async override Task<bool> CanPublishAsync(NotificationInfo notification, CancellationToken cancellationToken = default)
{
@ -83,47 +54,15 @@ public class WxPusherNotificationPublishProvider : NotificationPublishProvider
}
var contentType = notificationDefine?.GetContentTypeOrDefault(MessageContentType.Text)
?? MessageContentType.Text;
if (!notification.Data.NeedLocalizer())
{
var title = notification.Data.TryGetData("title").ToString();
var message = notification.Data.TryGetData("message").ToString();
await WxPusherMessageSender.SendAsync(
content: message,
summary: title,
contentType: contentType,
topicIds: topics,
uids: uids,
url: url,
cancellationToken: cancellationToken);
}
else
{
var titleInfo = notification.Data.TryGetData("title").As<LocalizableStringInfo>();
var titleResource = GetResource(titleInfo.ResourceName);
var titleLocalizer = await LocalizerFactory.CreateByResourceNameAsync(titleResource.ResourceName);
var title = titleLocalizer[titleInfo.Name, titleInfo.Values].Value;
var messageInfo = notification.Data.TryGetData("message").As<LocalizableStringInfo>();
var messageResource = GetResource(messageInfo.ResourceName);
var messageLocalizer = await LocalizerFactory.CreateByResourceNameAsync(messageResource.ResourceName);
var message = messageLocalizer[messageInfo.Name, messageInfo.Values].Value;
await WxPusherMessageSender.SendAsync(
content: message,
summary: title,
contentType: contentType,
topicIds: topics,
uids: uids,
url: url,
cancellationToken: cancellationToken);
}
}
private LocalizationResourceBase GetResource(string resourceName)
{
return LocalizationOptions.Resources.Values
.First(x => x.ResourceName.Equals(resourceName));
var notificationData = await NotificationDataSerializer.ToStandard(notification.Data);
await WxPusherMessageSender.SendAsync(
content: notificationData.Message,
summary: notificationData.Title,
contentType: contentType,
topicIds: topics,
uids: uids,
url: url,
cancellationToken: cancellationToken);
}
}

72
aspnet-core/modules/realtime-notifications/LINGYUN.Abp.Notifications/LINGYUN/Abp/Notifications/DefaultNotificationDataSerializer.cs

@ -1,5 +1,8 @@
using LINGYUN.Abp.RealTime.Localization;
using Microsoft.Extensions.Localization;
using Newtonsoft.Json;
using System;
using System.Threading.Tasks;
using Volo.Abp.DependencyInjection;
namespace LINGYUN.Abp.Notifications;
@ -7,7 +10,13 @@ namespace LINGYUN.Abp.Notifications;
[Dependency(TryRegister = true)]
public class DefaultNotificationDataSerializer : INotificationDataSerializer, ISingletonDependency
{
public NotificationData Serialize(NotificationData source)
private readonly IStringLocalizerFactory _localizerFactory;
public DefaultNotificationDataSerializer(IStringLocalizerFactory localizerFactory)
{
_localizerFactory = localizerFactory;
}
public virtual NotificationData Serialize(NotificationData source)
{
if (source != null)
{
@ -44,4 +53,65 @@ public class DefaultNotificationDataSerializer : INotificationDataSerializer, IS
}
return source;
}
public async virtual Task<NotificationStandardData> ToStandard(NotificationData source)
{
var title = "";
var message = "";
var description = "";
if (!source.NeedLocalizer())
{
title = source.TryGetData("title").ToString();
message = source.TryGetData("message").ToString();
description = source.TryGetData("description")?.ToString() ?? "";
}
else
{
var titleInfo = source.TryGetData("title").As<LocalizableStringInfo>();
var titleLocalizer = await _localizerFactory.CreateByResourceNameAsync(titleInfo.ResourceName);
title = titleLocalizer[titleInfo.Name].Value;
if (titleInfo.Values != null)
{
foreach (var formatValue in titleInfo.Values)
{
if (formatValue.Key != null && formatValue.Value != null)
{
title = title.Replace($"{{{formatValue.Key}}}", formatValue.Value.ToString());
}
}
}
var messageInfo = source.TryGetData("message").As<LocalizableStringInfo>();
var messageLocalizer = await _localizerFactory.CreateByResourceNameAsync(messageInfo.ResourceName);
message = messageLocalizer[messageInfo.Name].Value;
if (messageInfo.Values != null)
{
foreach (var formatValue in messageInfo.Values)
{
if (formatValue.Key != null && formatValue.Value != null)
{
message = message.Replace($"{{{formatValue.Key}}}", formatValue.Value.ToString());
}
}
}
var descriptionInfo = source.TryGetData("description")?.As<LocalizableStringInfo>();
if (descriptionInfo != null)
{
var descriptionLocalizer = await _localizerFactory.CreateByResourceNameAsync(descriptionInfo.ResourceName);
description = descriptionLocalizer[descriptionInfo.Name].Value;
if (descriptionInfo.Values != null)
{
foreach (var formatValue in descriptionInfo.Values)
{
if (formatValue.Key != null && formatValue.Value != null)
{
description = description.Replace($"{{{formatValue.Key}}}", formatValue.Value.ToString());
}
}
}
}
}
return new NotificationStandardData(title, message, description);
}
}

5
aspnet-core/modules/realtime-notifications/LINGYUN.Abp.Notifications/LINGYUN/Abp/Notifications/INotificationDataSerializer.cs

@ -1,5 +1,8 @@
namespace LINGYUN.Abp.Notifications;
using System.Threading.Tasks;
namespace LINGYUN.Abp.Notifications;
public interface INotificationDataSerializer
{
NotificationData Serialize(NotificationData source);
Task<NotificationStandardData> ToStandard(NotificationData source);
}

Loading…
Cancel
Save