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.
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.
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.
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.