How to display a custom admin system notification in the admin panel Magento 2?

In this article, we will be guiding you on how to display a custom admin system message in the backend.

One of the most useful default functionality is the admin notification. Magento uses this functionality to notify admin user of various errors, warning, upgrade and patch information. Whenever you log in to the admin panel, such notifications may appear giving you some vital information. These notifications can vary from messages about invalidated indexes to vulnerability issues. Adding custom system messages can be helpful and we are going to show you how to do it in a few simple steps.

There are four types of system admin notification that Magento uses which are listed below.

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

Here we are using Aureatelabs as the Vendor name and CustomNotification as the name of the module.  You can change this according to your Vendor and Module name.

First you have to create a di.xml file in app/code/Aureatelabs/CustomNotification/etc/adminhtml/ directory

<?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="customMessageNotification" xsi:type="string">Aureatelabs\CustomNotification\Model\System\Message\CustomMessageNotification</item>
           </argument>
       </arguments>
   </type>
</config>

Next we will be creating CustomMessageNotification.php in  app/code/Aureatelabs/CustomNotification/Model/System/Message directory.

This class must implement the "\Magento\Framework\Notification\MessageInterface" interface.

<?php
namespace Aureatelabs\CustomNotification\Model\System\Message;

use Magento\Framework\Notification\MessageInterface;

class CustomMessageNotification implements MessageInterface {

   /**
    * Message identity
    */
   const MESSAGE_IDENTITY = 'custom_system_notification';

   /**
    * Retrieve unique system message identity
    *
    * @return string
    */
   public function getIdentity()
   {
       return self::MESSAGE_IDENTITY;
   }

   /**
    * Check whether the system message should be shown
    *
    * @return bool
    */
   public function isDisplayed()
   {
       // return true will show the system notification, here you have to check your condition to display notification and base on that return true or false
       return true;
   }

   /**
    * Retrieve system message text
    *
    * @return \Magento\Framework\Phrase
    */
   public function getText()
   {
       return __('Custom System Message Notification Text.');
   }

   /**
    * Retrieve system message severity
    * Possible default system message types:
    * - MessageInterface::SEVERITY_CRITICAL
    * - MessageInterface::SEVERITY_MAJOR
    * - MessageInterface::SEVERITY_MINOR
    * - MessageInterface::SEVERITY_NOTICE
    *
    * @return int
    */
   public function getSeverity()
   {
       return self::SEVERITY_MAJOR;
   }

}

Possible default system message types are

SEVERITY_CRITICAL – to add a critical message
SEVERITY_MAJOR – to add a major message
SEVERITY_MAJOR – to add a major message
SEVERITY_MINOR – to add a minor message
SEVERITY_NOTICE – to add a notice

 Now, execute the two commands given below:

php bin/magento setup:di:compile
php bin/magento cache:flush

And that’s it. Log in into the admin panel and you will see a custom system admin message notification.

Custom system admin message notification screenshot

  • Share :