nsIAlertsService 编辑

toolkit/components/alerts/nsIAlertsService.idlScriptable This interface can be used to notify the user of something that does not require an immediate reaction. For example, it can be used to notify the user that their downloads are complete or that they have new mail. 1.0 66 Introduced Gecko 1.7 Inherits from: nsISupports Last changed in Gecko 22 (Firefox 22 / Thunderbird 22 / SeaMonkey 2.19)

A notification displayed by the alerts serviceBy 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.

Note: Prior Firefox 22, the alerts service was only supported on Windows in Gecko 1.7, had no effect on Mac OS X in Gecko 1.8, but was fully supported in Mac OS X in Gecko 1.9. Note:  Since Gecko 22, the notifications are provided by Firefox and have become platform-independent.

Implemented by: @mozilla.org/alerts-service;1 as a service:

var alertsService = Components.classes["@mozilla.org/alerts-service;1"]
                    .getService(Components.interfaces.nsIAlertsService);

Method overview

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);

Methods

showAlertNotification()

Displays a notification window.

Note: If you are calling this function from JavaScript, you should wrap it in a try/catch because it can fail on Mac OS X prior to Firefox 22.
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
);
Parameters
imageUrl
A URL identifying the image to display in the notification alert.
title
The title for the alert.
text
The text to display in the alert, explaining the alert condition. The text must not be too long, otherwise it might be truncated to a platform-specific length. If the text is too long, try to use line returns in the text to have it split and displayed over multiple lines.
textClickable Optional
If true, 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 the
text was made to look like a link.
cookie Optional
A blind cookie the alert passes back to the consumer during alert listener callbacks.
alertListener Optional
An object to receive callbacks from the alert; may be null if you don't care about callbacks. It must implement an observe() 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
A name for the notification; this is not displayed in the alert. However, certain notification systems (such as Growl on Mac OS X) allow the user to disable alerts by name. This name is used in that configuration panel. On Android the name is hashed and used as a notification ID.
dir Optional
Bidi override for the title. Valid values are "auto", "ltr" or "rtl". Only available on supported platforms.
lang Optional
Language of title and text of the alert. Only available on supported platforms.
data Optional
The value of the cookie parameter passed to showAlertNotification.
principal Optional
The principal of the page that created the alert. Used for IPC security checks, and to determine whether the alert is actionable.
inPrivateBrowsing Optional
Controls the image loading behavior. If true, the image URL will be loaded in private browsing mode.
Exceptions thrown
NS_ERROR_NOT_AVAILABLE
Unable to display the notification; this may happen, for example, on Mac OS X if Growl is not installed.

Example

Simple usage

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
}

Listening for callbacks

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
}

Example 2

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');

closeAlert()

Closes alerts created by the service.

void closeAlert(
  in AString name, Optional
  in nsIPrincipal principal Optional
);
Parameters
name
The name of the notification to close. If no name is provided then only a notification created with no name (if any) will be closed.
principal
The title for the alert.

 

See also

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据

词条统计

浏览:46 次

字数:10780

最后编辑:6 年前

编辑次数:0 次

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文