2

am trying to insert custom notifications into adminnotification_inbox table as per core module Magento-AdminNotifications approache but am't able to insert custom notifications. Code is in below

events.xml

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
<event name="controller_action_predispatch">
    <observer name="adminnotification" instance="MyNameSpace\MyModuleName\Observer\PredispatchAdminActionControllerObserver" />
</event>

NameSpace/ModuleName/Model/Feed.php

<?php namespace  ConversionBug\Core\Model;             

Class Feed extends \Magento\AdminNotification\Model\Feed{

  public function __construct(\Psr\Log\LoggerInterface $logger, 
                              \Magento\Framework\Stdlib\DateTime\DateTime $date) {
    $this->_logger = $logger;        
    $this->date = $date;

}
  public function checkUpdate() {
    $this->_logger->debug("Feed Message here");
    $writer = new \Zend\Log\Writer\Stream(BP . '/var/log/test.log');
    $logger = new \Zend\Log\Logger();
    $logger->addWriter($writer);
    $logger->info('Your text message');
    $this->logger->debug($message);
  }

}

1
  • Your code only shows attempts to log message. What did you try to insert notifications ? Commented Aug 20, 2016 at 9:37

1 Answer 1

1

First you need to inject Magento/AdminNotification/Model/InboxFactory in your constructor:

protected $_inboxFactory;

public function __construct(\Psr\Log\LoggerInterface $logger, 
                              \Magento\Framework\Stdlib\DateTime\DateTime $date,
                              \Magento\AdminNotification\Model\InboxFactory $inboxFactory) {
    $this->_logger = $logger;        
    $this->date = $date;
    $this->_inboxFactory = $inboxFactory;
}

Then you will need to have data stored in an array like this:

$data = [
    'severity' => $severity,
    'date_added' => $itemPublicationDate,
    'title' => $title,
    'description' => $description,
    'url' => $link
];

Finally you can insert your admin notification in the database by calling:

$this->_inboxFactory->create()->parse($data);

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.