Magento as an E-commerce platform has the power to extend the native core functionalities which keep Magento away from the competition. One of the most effective default functionality is admin notifications. Magento uses admin notifications in the store backend to notify admin about various upgrades, patches, notice or errors which is required to grab the attention.
Whenever you login to admin panel, such notifications may appear giving you some important information. Have you ever think about adding custom notification in your Magento 2 extensions? You may require to add custom notification in Magento 2 to inform admin about latest upgrade, installation information, news related to it or any useful piece of content to get them notified about. Default Magento u2 contains 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
Recently we came across the requirement of Adding Custom Admin Notification in Magento 2 and we nailed the solution by developing custom code for it. Today we’re sharing the same code to help you out adding your custom notification in your own module .
Firstly, you need to create “di.xml” file at App\code\Vendor\Extension\etc\adminhtml with below code.
<pre class="lang:default decode:true"> <?xml version="1.0" encoding="utf-8"?> <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="AdminQuoteMessages" xsi:type="string">Vendor\Extension\Model\Admin\Quote\Messages</item> </argument> </arguments> </type> </config> </pre>
Once the file is created, you need to create another file at App\code\Vendor\Extension\Model\Admin\Quote\ with below code and name it as “Messages.php”.
<pre class="lang:default decode:true"> <?php namespace Vendor\Extension\Model\Admin\Quote; use Magento\Security\Model\ResourceModel\AdminSessionInfo\Collection; use Magento\Backend\Model\UrlInterface; use Magento\Backend\Model\Auth\Session; use Magento\Framework\Notification\MessageInterface; class Messages implements MessageInterface { protected $backendUrl; private $adminSessionInfoCollection; protected $authSession; public function __construct( Collection $adminSessionInfoCollection, UrlInterface $backendUrl, Session $authSession ) { $this->authSession = $authSession; $this->backendUrl = $backendUrl; $this->adminSessionInfoCollection = $adminSessionInfoCollection; } public function getText() { $message = __('Admin notification Add Successfully.'); return $message; } public function getIdentity() { return md5('VENDOR_EXTENSION' . $this->authSession->getUser()->getLogdate()); } public function isDisplayed() { return true; } public function getSeverity() { // From here you can change notification message type. return \Magento\Framework\Notification\MessageInterface::SEVERITY_CRITICAL; } } </pre>
Woot, you have successfully added admin notification. Not only this, you can customize this code to meet certain conditions as per your need. You can even change notification type like default Magento by using following values to your code.
SEVERITY_CRITICAL– to add critical message
SEVERITY_MAJOR– to add major message
SEVERITY_MINOR– to add minor message
SEVERITY_NOTICE– to add notice
I hope you can now add Custom Admin Notification in Magento 2 using the above piece of code. But If you still have any confusions or questions regarding this code, just leave a comment below and I will get back to you! Don’t forget to hit the stars and share the blog with other magento folks.
Until that, Happy Coding!