How to show notification in UI from custom module

I have custom module and wish to show some notifications to users of Xibo UI regarding certain events.
Is there any way to do it? if yes, how and example code will be very helpful…
Thank you

This should be possible - although i’ve not tried it :slight_smile: you would need to use the event dispatcher to raise a custom event, and add some middleware to catch that event and create the notification.

In the custom folder, create two classes - MyNotificationMiddleware and MyNotificationEvent

MyNotificationEvent

namespace Xibo\Custom;
class MyNotificationEvent extends Event
{
    const NAME = 'my.notification.event';
    public $module;

    public function __construct($module)
    {
        $this->module = $module;
    }
}

MyNotificationMiddleware

namespace Xibo\Custom;
class MyMiddleware extends Middleware
{
    public function call()
    {
        $app = $this->getApplication();

        $app->hook('slim.before.dispatch', function() use($app) {
            /** @var EventDispatcher $dispatcher */
            $dispatcher = $app->dispatcher;
            $dispatcher->addListener(MyNotificationEvent::NAME, function(Event $event) use ($app) {
                /** @var MyNotificationEvent $event */
                $module = $event->module;
         
               // Use the module information to create a notification with the notificationFactory

                /** @var NotificactionFactory $notificationFactory */
                $notificationFactory = $app->notificationFactory;
                $notification = $this->notificationFactory->createEmpty();
                $notification->subject = '';
                $notification->body = '';
                $notification->createdDt = $app->dateService->getLocalDate(null, 'U');
                $notification->releaseDt = $app->dateService->getLocalDate(null, 'U');
                $notification->isEmail = 0;
                $notification->isInterrupt = 0;
                $notification->userId = $app->user->userId;
                $notification->assignUserGroup(<userGroupId>);
                $notification->save();
            });
        });

        // Next middleware
        $this->next->call();
    }
}

Your Module

In your module, at appropriate times, use getDispatcher to create and raise your event.

// Create the event and pass it your module
$event = new MyNotificationEvent($this);
$this->getDispatcher()->dispatch($event::NAME, $event);

settings.php

Wire up your new middleware in settings.php

// Additional Middleware
$middleware = [new \Xibo\Custom\MyNotificationMiddleware()];
1 Like