4

I want to create a custom admin notification message for a module im developing like the one on the image below. I want the notification to remain visible in all admin pages until a certain condition has been met.

enter image description here

There is another thread on this subject but i couldn't find how to get the model class that implements \Magento\Framework\Notification\MessageInterface to be executed. The thread author mentioned that a dependency to Magento_AdminNotification is needed.

Is that achieved on the di.xml file? A more detailed explanation would be very helpful.

1
  • I found that adding <item name="className" xsi:type="string">VendorName\ModuleName\Model\ClassName</item>to module-admin-notification's di.xml will get the notification to show up. But i want the code to be self contained in my module directory since i want my module to be distributed to other people. Commented Jun 3, 2016 at 10:40

2 Answers 2

7

I figured out how to do it. I was writing the di.xml file on the wrong directory.

To get your module's custom notification class to run, you need to create the file VendorName/ModuleName/etc/adminhtml/di.xml with the following code:

<?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="yourClassName" xsi:type="string">VendorName\ModuleName\Model\System\Message\YourClassName</item>
            </argument>
        </arguments>
    </type>
</config>

Then you have to create the VendorName\ModuleName\Model\System\Message\YourClassName.php class file with the following code:

<?php

namespace VendorName\ModuleName\Model\System\Message;

class YourClassName 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()
    {
        // Retrieve message text
        return 'Notification message text goes here';
    }

    public function getSeverity()
    {
        // Possible values: SEVERITY_CRITICAL, SEVERITY_MAJOR, SEVERITY_MINOR, SEVERITY_NOTICE
        return self::SEVERITY_MAJOR;
    }
}
3
  • Hi @Daniel, I need to understand how to trigger this message, i want to send a notification on some condition in my module. Commented Oct 2, 2018 at 9:20
  • @Daniel I have tried code but not working in magento 2.2.6 Commented Dec 8, 2018 at 5:12
  • @Daniel what about if we want to add multiple notification using single file Commented Oct 19, 2019 at 10:55
1

Ktpl/Ordermanagement/etc/adminhtml/di.xml

<?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="user_view_count" xsi:type="string">Ktpl\Ordermanagement\Model\System\Message\Userviewcount</item>
            </argument>
        </arguments>
    </type>
</config>

Ktpl\Ordermanagement\Model\System\Message\Userviewcount.php

<?php

namespace Ktpl\Ordermanagement\Model\System\Message;

class Userviewcount implements \Magento\Framework\Notification\MessageInterface
{

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

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

   /**
    * Check whether the system message should be shown
    *
    * @return bool
    */
   public function isDisplayed()
   {
       // The message will be shown
       return true;
   }

   /**
    * Retrieve system message text
    *
    * @return \Magento\Framework\Phrase
    */
   public function getText()
   {
       return __('System Message 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;
   }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.