DNN 将文件附加到通知

DNN attach file to Notification

我正在使用 DNN 9,我想发送带有附件的通知,但 DNN 似乎不允许这样做。

有没有办法(或任何解决方法)做到这一点?

这是 NotificationsController 的 DNN 代码

这是我调用 DNN 代码的代码

//...
Notification dnnNotification = new Notification
{
    NotificationTypeID = notification.NotificationTypeId,
    From = notification.From,
    Subject = notification.Subject,
    Body = notification.Body
};
NotificationsController.Instance.SendNotification(dnnNotification, portalId, dnnRoles, dnnUsers);

您不能将文件附加到 DNN 中的通知。但是,您可以将自定义通知操作添加到通知类型。这些操作会导致在通知下添加 links(就像默认的 "Dismiss" 操作将通知标记为 "read")。

为了发送通知,您需要创建一个 NotificationType 来关联它。 NotificationTypeAction 被添加到类型中。因此,无论何时您发送某种类型的通知,操作都会随之发生。

您可以创建一个 NotificationTypeAction 并将其命名为 "Download Attachment"。当用户单击 link 时,它将调用自定义 api 服务。该服务可以提供文件。

下面是一些示例代码,我在其中创建了一个带有 1 个自定义操作的自定义类型:

public void AddNotificationType()
{
    var actions = new List<NotificationTypeAction>();
    var deskModuleId = DesktopModuleController.GetDesktopModuleByFriendlyName(Constants.DESKTOPMODULE_FRIENDLYNAME).DesktopModuleID;

    var objNotificationType = new NotificationType
    {
        Name = Constants.NOTIFICATION_FILEDOWNLOAD,
        Description = "Get File Attachment",
        DesktopModuleId = deskModuleId
    };

    if (NotificationsController.Instance.GetNotificationType(objNotificationType.Name) == null)
    {
        var objAction = new NotificationTypeAction
        {
            NameResourceKey = "DownloadAttachment",
            DescriptionResourceKey = "DownloadAttachment_Desc",
            APICall = "DesktopModules/MyCustomModule/API/mynotification/downloadfile",
            Order = 1
        };
        actions.Add(objAction);

        NotificationsController.Instance.CreateNotificationType(objNotificationType);
        NotificationsController.Instance.SetNotificationTypeActions(actions, objNotificationType.NotificationTypeId);
    }
}

然后使用如下代码发送通知:

public void SendNotification(UserInfo userToReceive)
{
    // Get the notification type; if it doesn't exist, create it
    ModuleController mCtrl = new ModuleController();
    var itemAddedNType = NotificationsController.Instance.GetNotificationType(Constants.NOTIFICATION_FILEDOWNLOAD);
    if (itemAddedNType == null) 
    { 
        AddNotificationType();
        itemAddedNType = NotificationsController.Instance.GetNotificationType(Constants.NOTIFICATION_FILEDOWNLOAD);
    }


    if (itemAddedNType != null)
    {
        Notification msg = new Notification
        {
            NotificationTypeID = itemAddedNType.NotificationTypeId,
            Subject = "A file is ready to download.",
            Body = alertBody,
            ExpirationDate = DateTime.MaxValue,
            IncludeDismissAction = true,
        };

        List<UserInfo> sendUsers = new List<UserInfo>();
        sendUsers.Add(userToReceive);

        NotificationsController.Instance.SendNotification(msg, itemModule.PortalID, null, sendUsers);
    }
}

有关 DNN 通知的完整教程,我强烈建议您订阅 DNNHero.com 并观看这个包含示例代码的 3 部分系列。

https://www.dnnhero.com/Premium/Tutorial/ArticleID/265/DNN-Notifications-Introduction-Part-1-3