--- title: notifications(通知) slug: Mozilla/Add-ons/SDK/High-Level_APIs/notifications tags: - Add-on SDK - 通知 translation_of: Archive/Add-ons/Add-on_SDK/High-Level_APIs/notifications ---
{{AddonSidebar}}
Stable
向用户展示短暂的 toaster 风格的桌面消息。
本 API 支持Windows、使用Growl(或者像OS X 10.9 Mavericks那样的通知中心)的 OS X 的桌面通知,以及使用 libnotify 的Linux系统
这儿有个典型的例子。当消息被点击,控制台上回记录一个字符串。
var notifications = require("sdk/notifications");
notifications.notify({
title: "Jabberwocky",
text: "'Twas brillig, and the slithy toves",
data: "did gyre and gimble in the wabe",
onClick: function (data) {
console.log(data);
// console.log(this.data) would produce the same result.
}
});
下面这个示例用来展示一个保存在 add-on 的 data 目录下的图标。参看 self 模块文档以获取更多信息。
var notifications = require("sdk/notifications");
var self = require("sdk/self");
var myIconURL = self.data.url("myIcon.png");
notifications.notify({
text: "I have an icon!",
iconURL: myIconURL
});
从 Firefox 34 起,你能使用 "./myIcon.png" 作为 self.data.url("myIcon.png") 的别名。所以你也可以把上面的代码重写成这样:
var notifications = require("sdk/notifications");
var myIconURL = "./myIcon.png";
notifications.notify({
text: "I have an icon!",
iconURL: myIconURL
});
本模块依赖于底层系统的通知服务。如果用户的系统不支持桌面通知或者通知服务没有运行:
notify(options)向用户展示一个短暂的通知
options : object
可选项:
| Name | Type | |
|---|---|---|
| title | string |
作为消息标题的字符串。 |
| text | 作为消息体的字符串。 |
|
| iconURL | string |
消息里的图标的 URL 。可以是个远程的、本地的或者使用 |
| onClick | function |
用户点击消息是调用的函数。它会传递一个 |
| data | string |
传递给 |