Nick Jones

a magento developer at Meanbee (@nickj89) and uk magento expert based in Bath

Magento How-to: Custom Admin Notifications

Magento have a pretty neat system going where they pull the latest news from an RSS feed hosted on their website – http://www.magentocommerce.com/notifications_feed to be precise. I thought it would be interesting to get this working for custom modules built by the developer community, although I feel it’s important not to abuse this system as it is a very obtrusive method of getting a message to your users.

Making the Model
All of the code to do this is written for us. We can store our messages in the same table that Magento uses to store theirs. The model we are after is Mage_AdminNotification_Model_Feed. All we need to do is extend it and change the RSS feed url.

<?php
class Meanbee_Notify_Model_Feed extends Mage_AdminNotification_Model_Feed {
    public function getFeedUrl() {
        $url =
            Mage::getStoreConfigFlag(self::XML_USE_HTTPS_PATH) ? 'https://' : 'http://'
            . 'www.meanbee.com/path/to/your/file.rss';
    }
 
    public function observe() {
        $model  = Mage::getModel('notify/feed');
        $model->checkUpdate();
    }
}
?>

We are overriding the getFeedUrl() method that was defined in Mage_AdminNotification_Model_Feed. We are simply using our own URL along with deciding which protocol to use for the communication. I’d advise sticking to the administrator’s wishes with regards to using https or not.

We define an observe() method, to be called by the next section, grabbing the feed contents and entering them into the database if they are new.

Inheritance lets us.. well.. inherit the remaining methods are we need to get it working, including the parsing of the XML and putting it into the database. Magic.

Observing an Admin Login
Magento makes us of the Observer Design Pattern meaning that we are able to observe events that are happening in the system and have Magento call methods for us when said events occur. We are looking for this event to fire off whenever a user is logged into the administration area (throttling is handled by Mage_AdminNotification_Model_Feed), specifically the controller_action_predispatch hook, as the events are more commonly called.

As with everything when developing modules, observing events are controlled through a module’s configuration file.

<?xml version="1.0" encoding="UTF-8"?>
<config>
    ...
    <adminhtml>
        ...
        <events>
            <controller_action_predispatch>
                <observers>
                    <meanbee_notification>
                        <type>singleton</type>
                        <class>notify/feed</class>
                        <method>observe</method>
                    </meanbee_notification>
                </observers>
            </controller_action_predispatch>
        </events>
        ...
    </adminhtml>
    ...
</config>

This post is not intended as a tutorial into creating a module so I’ll not explain the use of notify in <class>, it would have been declared as the model prefix earlier on in the config.xml.

RSS Feed Format
Below is an excerpt from the Magento feed – the misspelling of the Magento website is actually there.

<?xml version="1.0" encoding="utf-8" ?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
    <channel>
        <atom:link href="http://www.magentocommerce.com/notifications_feed" rel="self" type="application/rss+xml" />
        <title>MagentoCommerce</title>
        <link>http://www.magentocommerc.com/</link>
        <description>MagentoCommerce</description>
        <copyright>Copyright (c) 2009 Varien</copyright>
        <webMaster>support@varien.com (Magento support)</webMaster>
        <language>en</language>
        <lastBuildDate>Thu May 14 12:00:01 2009</lastBuildDate>
        <ttl>300</ttl>
        <item>
            <title>Magento version 1.3.1 now available</title>
            <link>http://www.magentocommerce.com/blog/comments/magento-version-131-now-available/</link>
            <severity>3</severity>
            <description><![CDATA[Magento version 1.3.1 is now available for download and upgrade. This version includes some issue resolutions for Magento version 1.3.x and new features such as Checkout By Amazon and Amazon Flexible Payment. To see a full list of updates please check the release notes page.]]></description>
            <pubDate>Sat Apr 18 04:06:02 2009</pubDate>
        </item>
    </channel>
</rss>

Dare I say nothing but the <item>s are important. Magento does not use the <lastBuildDate> for anything as it uses its own throttling mechanism.

And we’re done.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" highlight="">