nsISupports
Last changed in Gecko 22 (Firefox 22 / Thunderbird 22 / SeaMonkey 2.19)By default a message is displayed in a small window that slides up from the bottom of the screen, holds there for a few seconds, then slides down. The specific appearance varies from platform to platform.
Implemented by: @mozilla.org/alerts-service;1
as a service:
var alertsService = Components.classes["@mozilla.org/alerts-service;1"] .getService(Components.interfaces.nsIAlertsService);
void showAlertNotification(in AString imageUrl, in AString title, in AString text, [optional] in boolean textClickable, [optional] in AString cookie, [optional] in nsIObserver alertListener, [optional] in AString name, [optional] in AString dir, [optional] in AString lang, [optional] in AString data, [optional] in nsIPrincipal principal,[optional] in boolean inPrivateBrowsing); |
void closeAlert([optional] in AString name, [optional] in nsIPrincipal principal); |
Displays a notification window.
void showAlertNotification( in AString imageUrl, in AString title, in AString text, in boolean textClickable, Optional in AString cookie, Optional in nsIObserver alertListener, Optional in AString name, Optional in AString dir, Optional in AString lang, Optional in AString data, Optional in nsIPrincipal principal, Optional in boolean inPrivateBrowsing, Optional );
imageUrl
title
text
textClickable
Optionaltrue
, if the user clicks on it, the listener is notified; when hovered the notification background becomes lighter and the cursor turns to a pointer. Prior to Gecko 32 thecookie
OptionalalertListener
Optionalobserve()
method that accepts three parameters:
subject
: always null.topic
: "alertfinished" when the alert disappears, "alertclickcallback" if the user clicks the text or "alertshow" (since Gecko 22) when the alert is shown.data
: The value of the cookie
parameter passed into the showAlertNotification
method.name
Optional dir
Optional lang
Optional data
Optionalprincipal
OptionalinPrivateBrowsing
OptionalNS_ERROR_NOT_AVAILABLE
The following code was used to display the above notification.
var alertsService = Components.classes["@mozilla.org/alerts-service;1"]. getService(Components.interfaces.nsIAlertsService); try { alertsService.showAlertNotification("chrome://mozapps/skin/downloads/downloadIcon.png", "Alert title", "Alert text goes here.", false, "", null, ""); } catch (e) { // This can fail on Mac OS X }
You can be notified when the notification window disappears or the user clicks on the message by passing an object implementing nsIObserver
as the alertListener
parameter:
var listener = { observe: function(subject, topic, data) { alert("subject=" + subject + ", topic=" + topic + ", data=" + data); } } var alertsService = Components.classes["@mozilla.org/alerts-service;1"]. getService(Components.interfaces.nsIAlertsService); try { alertsService.showAlertNotification("", "Alerts service test", "Click me", true, "cookie", listener, ""); } catch (e) { // This can fail on Mac OS X }
This example shows how to use all the available observer topics:
var as = Cc['@mozilla.org/alerts-service;1'].getService(Ci.nsIAlertsService); var notifListener = { observe: function(aSubject, aTopic, aData) { console.error('incoming notification observer:', aSubject, aTopic, aData); if (aTopic == 'alertclickcallback') { console.error('user clicked trying to throw click'); Services.prompt.alert(Services.wm.getMostRecentWindow('navigator:firefox'), 'focus firefox', 'will now focus fireox and then focus the tab'); } else if (aTopic == 'alertshow') { console.log('just showed notification'); } else if (aTopic == 'alertfinished') { console.log('just alertfinished') } } }; as.showAlertNotification('chrome://branding/content/icon64.png', 'StackOverflow - New Messages', 'There are ## new messages. Click here to focus the tab', true, null, notifListener, 'StackOverflow Notifier');
Closes alerts created by the service.
void closeAlert( in AString name, Optional in nsIPrincipal principal Optional );
name
principal