Categories: How ToMagento 2

How to Add Custom Admin Notifications in Magento 2

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!

 
Click to rate this post!
[Total: 10 Average: 4.5]
Dhiren Vasoya

Dhiren Vasoya is a Director and Co-founder at MageComp, Passionate 🎖️ Certified Magento Developer👨‍💻. He has more than 9 years of experience in Magento Development and completed 850+ projects to solve the most important E-commerce challenges. He is fond❤️ of coding and if he is not busy developing then you can find him at the cricket ground, hitting boundaries.🏏

Recent Posts

Magento 2: Add Quantity Increment and Decrement on Category Page

Hello Magento Friends, In this blog, we will discuss about adding quantity increment and decrement…

23 hours ago

How to Integrate ChatGPT with Laravel Application?

In this guide, we'll explore how to integrate ChatGPT, an AI-powered chatbot, with a Laravel…

4 days ago

What are Net Sales? How to Calculate Your Net Sales?

In the world of business, understanding financial metrics is crucial for making informed decisions and…

6 days ago

Magento 2 Extensions Digest April 2024 (New Release & Updates)

Welcome to the MageComp Monthly Digest, where we bring you the latest updates, releases, and…

6 days ago

The ABCs of Geofencing: Definition, Features and Uses

In this era, businesses are always on the lookout for ways to engage with their…

1 week ago

How to Delete Product Variant in a Shopify Remix App using GraphQL Mutations?

Managing a Shopify store efficiently involves keeping your product catalog organized. This includes removing outdated…

1 week ago