Browse Source

fix: get the channel from the user definition notification.

pull/659/head
cKey 3 years ago
parent
commit
2c5eed2c9c
  1. 35
      aspnet-core/modules/pushplus/LINGYUN.Abp.Notifications.PushPlus/LINGYUN/Abp/Notifications/NotificationDefinitionExtensions.cs
  2. 11
      aspnet-core/modules/pushplus/LINGYUN.Abp.Notifications.PushPlus/LINGYUN/Abp/Notifications/PushPlus/PushPlusNotificationPublishProvider.cs
  3. 78
      aspnet-core/modules/pushplus/LINGYUN.Abp.PushPlus/LINGYUN/Abp/PushPlus/Message/IPushPlusMessageSenderExtensions.cs

35
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
{
/// <summary>
/// 设定消息发送通道
/// </summary>
/// <param name="notification"></param>
/// <param name="channelType"></param>
/// <returns></returns>
public static NotificationDefinition WithChannel(
this NotificationDefinition notification,
PushPlusChannelType channelType)
{
return notification.WithProperty("channel", channelType);
}
/// <summary>
/// 获取消息发送通道
/// </summary>
/// <param name="notification"></param>
/// <param name="defaultChannelType"></param>
/// <returns></returns>
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;
}
/// <summary>
/// 消息群发群组编码
/// 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

11
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);
}
}

78
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<string> 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),
};
}
}
Loading…
Cancel
Save