3

How do you initialize a NotificationController in DNN 7.1.2?

I've tried:

var nc = new DotNetNuke.Services.Social.Notifications.NotificationController();

However this is empty and has no methods to call... Am I initializing the wrong thing?

Surely there should be something in there other than ToString, GetType, Equals and GetHashCode

I need to be able to create NotificationTypes and create Notifications.

Thanks

Alex
  • 1,549
  • 2
  • 16
  • 41

2 Answers2

5

You can use NotificationsController.Instance.SendNotification method to send notifications. Here is the example:

var notificationType = NotificationsController.Instance.GetNotificationType("HtmlNotification");
var portalSettings = PortalController.GetCurrentPortalSettings();
var sender = UserController.GetUserById(portalSettings.PortalId, portalSettings.AdministratorId);

var notification = new Notification {NotificationTypeID = notificationType.NotificationTypeId, Subject = subject, Body = body, IncludeDismissAction = true, SenderUserID = sender.UserID};
NotificationsController.Instance.SendNotification(notification, portalSettings.PortalId, null, new List<UserInfo> { user });

This will send notification to a specific user.

bdukes
  • 152,002
  • 23
  • 148
  • 175
Prashant Lakhlani
  • 5,758
  • 5
  • 25
  • 39
  • 1
    `NotificationsController` uses DNN's (kind of weird) `ServiceLocator` pattern, where it's just a static container which gives you an instance of an interface when you call the static `Instance` method. It's mainly there to support testability; there's a static `SetTestableInstance` method you can call if to get an alternate instance in your tests. There are a lot of types that follow this pattern throughout DNN (and many of the older controllers which don't follow the pattern have a `Testable*Controller` counterpart which does). – bdukes Nov 13 '13 at 22:39
1

If you need to create your own Notification type use the code below as a guide, it will create a NotificationType "GroupApprovedNotification". This is from core groups module.

type = new NotificationType { Name = "GroupApprovedNotification", Description = "Group Approved Notification", DesktopModuleId = deskModuleId };
if (NotificationsController.Instance.GetNotificationType(type.Name) == null)
{
    NotificationsController.Instance.CreateNotificationType(type);

}
vreboton
  • 41
  • 5