Magento is an e-commerce platform capable of expanding core functions. One of the very useful functions is administrator notification. Magento uses this function in the backend to notify administrators of updates, upgrades, notification patches or bugs that need to be fixed to improve the user experience. Today, this blog will show you how to create custom Admin System Messages and Admin Notification Messages.

A. Add Custom Admin System Message in Magento 2

Step 1: di.xml

We start by adding a new item into the message list, which will point to the corresponding model class. It can be done via the Dependency Injection.

Add the following “type” node into “app/code/Magenest/Notification/etc/adminhtml/di.xml”, a file of your module as shown below.

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
   <type name="Magento\Framework\Notification\MessageList">
       <arguments>
           <argument name="messages" xsi:type="array">
               <item name="Notification" xsi:type="string">Magenest\Notification\Model\Message\Notification</item>
           </argument>
       </arguments>
   </type>
</config>

Step 2: Notification.php

Now we will create a Message Model Class itself, this class must implement the "\Magento\Framework\Notification\MessageInterface" interface.

Create app/code/Magenest/Notification/Model/Message folder. In this folder, create a Notification.php file with the following content:

<?php
namespace Magenest\Notification\Model\Message;
class Notification implements \Magento\Framework\Notification\MessageInterface
{
   public function getIdentity()
   {
       // Retrieve unique message identity
       return 'identity';
   }
   public function isDisplayed()
   {
       // Return true to show your message, false to hide it
       return true;
   }
   public function getText()
   {
       // message text

       return "Notification Add Successfully";
   }
   public function getSeverity()
   {
       // Possible values: 
       // SEVERITY_CRITICAL
       // SEVERITY_MAJOR
       // SEVERITY_MINOR
       // SEVERITY_NOTICE
       return self::SEVERITY_CRITICAL;
   }
}

You can see that there are four message types available. And each one is defined as a constant value:

  • Critical (SEVERITY_CRITICAL)
  • Major (SEVERITY_MAJOR)
  • Minor (SEVERITY_MINOR)
  • Notice (SEVERITY_NOTICE)

Clear cache of your Magento installation, login into the Admin page and you will see the result as shown below

Admin notification in Magento 2: Add Custom Admin System Message in Magento 2

B. Add Admin Notification Messages in Magento 2

And now we will cover another type of notifications that uses Default Admin NotifierNotifications.

We can add new admin notification using “Magento\Framework\Notification\NotifierInterface”.

protected $notifierPool;

   public function __construct(
       \Magento\Framework\Notification\NotifierInterface $notifierPool
   ) {
       $this->notifierPool = $notifierPool;
   }

   public function addNotification()
   {
       // link to detail notification page
       $NotificationUrl = $this->getUrl('adminhtml/');

       // Add notice
       $this->notifierPool->addNotice(
           'Wellcome to Magenest',
           'Message description text.',
           $NotificationUrl
       );

       // Add minor severity message
       $this->notifierPool->addMinor(
           'Message 1',
           'Message description text.',
           $NotificationUrl
       );

       // Add major severity message
       $this->notifierPool->addMajor(
           'Message 2',
           'Message description text.',
           $NotificationUrl
       );
       // Add critical severity message
       $this->notifierPool->addCritical(
           'Wellcome to Magenest',
           'Message description text.',
           $NotificationUrl
       );
   }
}

In the code above, four messages have been added using functions: addNotice, add Minor, addMajor, addCritical.

Clean the cache and go to the URL /magenest/notification/index in the browser, and go back to the dashboard, you will see the result as shown below:

Admin notification in Magento 2: Add Admin Notification Messages in Magento 2

And now, you can now log out and re-login into the admin page, You will see a message pop up as shown below:

Admin notification in Magento 2: Add Admin Notification Messages in Magento 2 - Popup

Thanks for reading! Follow our blog to read more useful Magento 2 guidelines.