How to Add Admin Notification Messages in Magento 2

Recently we have shared a tutorial on how to add custom Admin system messages in Magento 2. Today we will cover another type of notifications that uses Default Admin Notifier – Notifications.

The Notifications feature in Magento is used for notifying an admin user about variability issues, security patches, updates and other news.

There are four severity types of notification messages in Magento.

  • Notice – updates, releases and other Magento news;
  • Minor – minor updates and other messages;
  • Major – important notifications, which you should check shortly;
  • Critical – vulnerability issues and other most important notifications.

By default, Magento 2 uses only one notifier Magento\AdminNotification\Model\Inbox. So the notification messages that you can see in Admin panel are related to the default one. But Magento 2 provides an ability to have multiple notifiers. Each custom notifier must be registered in Notification Message List Magento\AdminNotification\Model\Inbox and implement the Notifier Interface Magento\Framework\Notification\NotifierInterface, so that Magento will know about all the existing notifiers and will use them.

If you examine the implementation of the Magento\Framework\Notification\NotifierPool class you will notice at least two interesting things.

The first one – the Notifier Pool class implements the Magento\Framework\Notification\NotifierInterface interface, as well as notifier classes do. So the following methods are available for each Notifier and for the Notifier Pool:

  • addCritical – adds a critical message;
  • addMajor – adds a major message;
  • addMinor – adds a minor message;
  • addNotice – adds a notice;
  • add – adds a new message with any severity specified, including custom ones.

Secondly, the Notifier Pool simply iterates through all registered Notifiers to process a message. So when you add a new message via Notifier Pool, the message will be processed by all of the available notifiers.

Let’s create a sample “Adminhtml” action to figure out how to add a message.

The definition of a new route named “atwix” for Atwix_AdminNotifications module is the following.

<?xml version="1.0"?>
<!--
/**
 * @author Atwix Team
 * @copyright Copyright (c) 2017 Atwix (https://www.atwix.com/)
 * @package Atwix_AdminNotifications
 */
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:App/etc/routes.xsd">
    <router id="admin">
        <route id="atwix">
            <module name="Atwix_AdminNotifications" before="Magento_Backend" />
        </route>
    </router>
</config>

And here is the new admin action class itself.

Note that there is a preference configured for Magento\Framework\Notification\NotifierInterface via Dependency Injection. It means that when you ask to instantiate a Notifier Interface, the Magento instantiates a Magento\Framework\Notification\NotifierPool object. That’s why the NotifierInterface is specified in the constructor method in our example class.

<?php
/**
 * @author Atwix Team
 * @copyright Copyright (c) 2017 Atwix (https://www.atwix.com/)
 * @package Atwix_AdminNotifications
 */

namespace Atwix\AdminNotifications\Controller\Adminhtml\SampleAdminNotification;

use Magento\Backend\App\AbstractAction as BackendAction;
use Magento\Backend\App\Action\Context as BackendActionContext;
use Magento\Framework\Controller\Result\Redirect as ResultRedirect;
use Magento\Framework\Notification\NotifierInterface as NotifierPool;

/**
 * Class Index
 */
class Index extends BackendAction
{
    /**
     * Notifier Pool
     *
     * @var NotifierPool
     */
    protected $notifierPool;

    /***
     * Index action constructor
     *
     * @param BackendActionContext $context
     * @param NotifierPool $notifierPool
     */
    public function __construct(
        BackendActionContext $context,
        NotifierPool $notifierPool
    ) {
        parent::__construct($context);
        $this->notifierPool = $notifierPool;
    }

    /**
     * Create Sample Notification Messages
     *
     * @return ResultRedirect
     */
    public function execute()
    {
        /**
         * Sample system config url will be used for "Read Details" link in notification message
         *
         * @var string $sampleUrl
         */
        $sampleUrl = $this->getUrl('adminhtml/sampleRoute/');

        // Add notice
        $this->notifierPool->addNotice('Notice Title', 'Notice description text.');

        // Add minor severity message
        $this->notifierPool->addMinor(
            'Minor Severity Message Title',
            // Add description text as unordered list
            [
                'First Message Description Item',
                'Second Message Description Item',
                'Third Message Description Item',
                'Fourth Message Description Item',
            ]
        );

        // Add major severity message
        $this->notifierPool->addMajor(
            'Major Severity Message Title',
            'Message description text.',
            // Add "Read Details" link
            $sampleUrl
        );

        // Add critical severity message
        $this->notifierPool->addCritical('Critical Severity Message Title', 'Message description text.');

        /** @var ResultRedirect $resultRedirect */
        $resultRedirect = $this->resultRedirectFactory->create();
        $resultRedirect->setPath('adminhtml/');

        return $resultRedirect;
    }
}

As you see from the example above – four notification messages were created.

After passing over the created action ({your_admin_url}/atwix/sampleAdminNotification/index/) in browser – four notification messages will be created. When you check the notification inbox – you will notice new unread messages.

Atwix Admin Notifications

Now you can create notifications for your Admin users. Next time we are going describe how to implement a custom admin notifier in Magento 2. Stay tuned!

Don’t skip