Notifications Magento 2:在管理中添加自定义通知

Notifications Magento 2:在管理中添加自定义通知,notifications,magento2,Notifications,Magento2,我看到一些模块有一个自定义通知,我的问题是如何将它添加到我的模块中 希望有人能帮助我。谢谢。要运行模块的自定义通知类,您需要使用以下代码创建文件VendorName/ModuleName/etc/adminhtml/di.xml: <?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magen

我看到一些模块有一个自定义通知,我的问题是如何将它添加到我的模块中


希望有人能帮助我。谢谢。

要运行模块的自定义通知类,您需要使用以下代码创建文件VendorName/ModuleName/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="yourClassName" xsi:type="string">VendorName\ModuleName\Model\System\Message\YourClassName</item>
            </argument>
        </arguments>
    </type>
</config>
<?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;
    }
}

VendorName\ModuleName\Model\System\Message\YourClassName
然后必须使用以下代码创建VendorName\ModuleName\Model\System\Message\YourClassName.php类文件:

<?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>
<?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;
    }
}

它会生成系统消息,但我需要弹出通知