From 2c5eed2c9cc5a653135668e697e83a5a74992185 Mon Sep 17 00:00:00 2001 From: cKey <35512826+colinin@users.noreply.github.com> Date: Thu, 25 Aug 2022 20:17:03 +0800 Subject: [PATCH] fix: get the channel from the user definition notification. --- .../NotificationDefinitionExtensions.cs | 35 ++++++++- .../PushPlusNotificationPublishProvider.cs | 11 ++- .../IPushPlusMessageSenderExtensions.cs | 78 +++++++++++++++++++ 3 files changed, 120 insertions(+), 4 deletions(-) create mode 100644 aspnet-core/modules/pushplus/LINGYUN.Abp.PushPlus/LINGYUN/Abp/PushPlus/Message/IPushPlusMessageSenderExtensions.cs diff --git a/aspnet-core/modules/pushplus/LINGYUN.Abp.Notifications.PushPlus/LINGYUN/Abp/Notifications/NotificationDefinitionExtensions.cs b/aspnet-core/modules/pushplus/LINGYUN.Abp.Notifications.PushPlus/LINGYUN/Abp/Notifications/NotificationDefinitionExtensions.cs index 8d93ece6c..765a2da2d 100644 --- a/aspnet-core/modules/pushplus/LINGYUN.Abp.Notifications.PushPlus/LINGYUN/Abp/Notifications/NotificationDefinitionExtensions.cs +++ b/aspnet-core/modules/pushplus/LINGYUN.Abp.Notifications.PushPlus/LINGYUN/Abp/Notifications/NotificationDefinitionExtensions.cs @@ -1,6 +1,39 @@ -namespace LINGYUN.Abp.Notifications; +using LINGYUN.Abp.PushPlus.Channel; +using System; + +namespace LINGYUN.Abp.Notifications; public static class NotificationDefinitionExtensions { + /// + /// 设定消息发送通道 + /// + /// + /// + /// + public static NotificationDefinition WithChannel( + this NotificationDefinition notification, + PushPlusChannelType channelType) + { + return notification.WithProperty("channel", channelType); + } + /// + /// 获取消息发送通道 + /// + /// + /// + /// + public static PushPlusChannelType GetChannelOrDefault( + this NotificationDefinition notification, + PushPlusChannelType defaultChannelType = PushPlusChannelType.WeChat) + { + if (notification.Properties.TryGetValue("channel", out var defineChannelType) == true && + defineChannelType is PushPlusChannelType channelType) + { + return channelType; + } + + return defaultChannelType; + } /// /// 消息群发群组编码 /// see: https://www.pushplus.plus/doc/guide/api.html#%E4%B8%80%E3%80%81%E5%8F%91%E9%80%81%E6%B6%88%E6%81%AF%E6%8E%A5%E5%8F%A3 diff --git a/aspnet-core/modules/pushplus/LINGYUN.Abp.Notifications.PushPlus/LINGYUN/Abp/Notifications/PushPlus/PushPlusNotificationPublishProvider.cs b/aspnet-core/modules/pushplus/LINGYUN.Abp.Notifications.PushPlus/LINGYUN/Abp/Notifications/PushPlus/PushPlusNotificationPublishProvider.cs index ec69e5363..1e1c75c97 100644 --- a/aspnet-core/modules/pushplus/LINGYUN.Abp.Notifications.PushPlus/LINGYUN/Abp/Notifications/PushPlus/PushPlusNotificationPublishProvider.cs +++ b/aspnet-core/modules/pushplus/LINGYUN.Abp.Notifications.PushPlus/LINGYUN/Abp/Notifications/PushPlus/PushPlusNotificationPublishProvider.cs @@ -1,4 +1,5 @@ -using LINGYUN.Abp.PushPlus.Message; +using LINGYUN.Abp.PushPlus.Channel; +using LINGYUN.Abp.PushPlus.Message; using LINGYUN.Abp.RealTime.Localization; using Microsoft.Extensions.Localization; using Microsoft.Extensions.Options; @@ -50,16 +51,19 @@ public class PushPlusNotificationPublishProvider : NotificationPublishProvider { topic = topicDefine; } + var channel = notificationDefine?.GetChannelOrDefault(PushPlusChannelType.Email) + ?? PushPlusChannelType.Email; if (!notification.Data.NeedLocalizer()) { var title = notification.Data.TryGetData("title").ToString(); var message = notification.Data.TryGetData("message").ToString(); - await PushPlusMessageSender.SendAsync( + await PushPlusMessageSender.SendWithChannelAsync( title, message, topic, + channelType: channel, cancellationToken: cancellationToken); } else @@ -72,10 +76,11 @@ public class PushPlusNotificationPublishProvider : NotificationPublishProvider var messageResource = GetResource(messageInfo.ResourceName); var message = LocalizerFactory.Create(messageResource.ResourceType)[messageInfo.Name, messageInfo.Values].Value; - await PushPlusMessageSender.SendAsync( + await PushPlusMessageSender.SendWithChannelAsync( title, message, topic, + channelType: channel, cancellationToken: cancellationToken); } } diff --git a/aspnet-core/modules/pushplus/LINGYUN.Abp.PushPlus/LINGYUN/Abp/PushPlus/Message/IPushPlusMessageSenderExtensions.cs b/aspnet-core/modules/pushplus/LINGYUN.Abp.PushPlus/LINGYUN/Abp/PushPlus/Message/IPushPlusMessageSenderExtensions.cs new file mode 100644 index 000000000..14e69fe3d --- /dev/null +++ b/aspnet-core/modules/pushplus/LINGYUN.Abp.PushPlus/LINGYUN/Abp/PushPlus/Message/IPushPlusMessageSenderExtensions.cs @@ -0,0 +1,78 @@ +using LINGYUN.Abp.PushPlus.Channel; +using System.Threading; +using System.Threading.Tasks; + +namespace LINGYUN.Abp.PushPlus.Message; + +public static class IPushPlusMessageSenderExtensions +{ + public static Task SendWithChannelAsync( + this IPushPlusMessageSender pushPlusMessageSender, + string title, + string content, + string topic = "", + PushPlusChannelType channelType = PushPlusChannelType.WeChat, + PushPlusMessageTemplate template = PushPlusMessageTemplate.Html, + string webhook = "", + string callbackUrl = "", + CancellationToken cancellationToken = default) + { + return channelType switch + { + PushPlusChannelType.WeChat => + pushPlusMessageSender.SendWeChatAsync( + title, + content, + topic, + template, + webhook, + callbackUrl, + cancellationToken), + PushPlusChannelType.WeWork => + pushPlusMessageSender.SendWeWorkAsync( + title, + content, + topic, + template, + webhook, + callbackUrl, + cancellationToken), + PushPlusChannelType.Webhook => + pushPlusMessageSender.SendWebhookAsync( + title, + content, + topic, + template, + webhook, + callbackUrl, + cancellationToken), + PushPlusChannelType.Email => + pushPlusMessageSender.SendEmailAsync( + title, + content, + topic, + template, + webhook, + callbackUrl, + cancellationToken), + PushPlusChannelType.Sms => + pushPlusMessageSender.SendSmsAsync( + title, + content, + topic, + template, + webhook, + callbackUrl, + cancellationToken), + _ => + pushPlusMessageSender.SendWeChatAsync( + title, + content, + topic, + template, + webhook, + callbackUrl, + cancellationToken), + }; + } +}