# 桌面通知
通知用戶發生了一些重要的事情。桌面通知會顯示在瀏覽器窗口之外。 下面的圖片是通知顯示時的效果,在不同的平臺下,通知的顯示效果會有些細微區別。
  
通常直接使用一小段 JavaScript 代碼創建通知,當然也可以通過擴展包內的一個單獨HTML頁面。
## 聲明
可以在 [extension manifest](manifest.html) 中聲明使用通知權限,像這樣:
```
{
"name": "My extension",
...
**"permissions": [
"notifications"
]**,
...
}
```
**注意:** 擴展聲明的 `notifications` 權限總是允許創建通知。 這樣申明之后就不再需要調用 `webkitNotifications.checkPermission()`。
## 與擴展頁面交互
擴展可以使用 [getBackgroundPage()](extension.html#method-getBackgroundPage) 和 [getViews()](extension.html#method-getViews)在通知與擴展頁面中建立交互。 例如:
```
// 在通知中調用擴展頁面方法...
chrome.extension.getBackgroundPage().doThing();
// 從擴展頁面調用通知的方法...
chrome.extension.getViews({type:"notification"}).forEach(function(win) {
win.doOtherThing();
});
```
## 例子
一個簡單的使用通知的例子,參見 [examples/api/notifications](http://src.chromium.org/viewvc/chrome/trunk/src/chrome/common/extensions/docs/examples/api/notifications/) 目錄。 更多的例子,以及在查看代碼中遇到的一些問題,請參見 [代碼例子](samples.html)。
也可以參考 html5rocks.com 的 [通知指南](http://www.html5rocks.com/tutorials/notifications/quick/)。 如果你只是聲明 "通知" 的權限,可以忽略權限相關的代碼,它不是必要的。
## API
擴展的桌面通知 API ,也可用于顯示一個網頁。 如以下代碼所示,首先創建一個簡單的文字通知或 HTML 通知,然后顯示通知。
```
// 創建一個簡單的文字通知:
var notification = webkitNotifications.createNotification(
'48.png', // icon url - can be relative
'Hello!', // notification title
'Lorem ipsum...' // notification body text
);
// 或者創建一個 HTML 通知:
var notification = webkitNotifications.createHTMLNotification(
'notification.html' // html url - can be relative
);
// 顯示通知
notification.show();
```
完整的 API 詳情,請參看 [Desktop notifications draft specification](http://dev.chromium.org/developers/design-documents/desktop-notifications/api-specification)。
## API reference: chrome.apiname
### Properties
<a></a>
#### getLastError
chrome.extensionlastError
### Methods
<a></a>
#### method name
void chrome.module.methodName(, ``)
Undocumented.
A description from the json schema def of the function goes here.
#### Parameters
#### Returns
#### Callback function
The callback _parameter_ should specify a function that looks like this:
If you specify the _callback_ parameter, it should specify a function that looks like this:
```
function(Type param1, Type param2) {...};
```
This function was added in version . If you require this function, the manifest key [minimum_chrome_version](manifest.html#minimum_chrome_version) can ensure that your extension won't be run in an earlier browser version.
### Events
<a></a>
#### event name
chrome.bookmarksonEvent.addListener(function(Type param1, Type param2) {...});
Undocumented.
A description from the json schema def of the event goes here.
#### Parameters
### Types
<a></a>
#### type name
- 基礎文檔
- 綜述
- 調試
- 格式:Manifest文件
- 模式匹配
- 改變瀏覽器外觀
- Browser Actions
- Context Menus
- 桌面通知
- Omnibox
- Override替代頁
- Page Actions
- 主題
- 與瀏覽器交互
- 書簽
- Cookies
- chrome.devtools.* APIs
- Events
- chrome.history
- Management
- 標簽
- 視窗
- 實現擴展
- 無障礙性(a11y)
- 背景頁
- Content Scripts
- 跨域 XMLHttpRequest 請求
- 國際化 (i18n)
- 消息傳遞
- Optional Permissions
- NPAPI 插件
- 完成并發布應用
- 自動升級
- 托管
- 打包
- 規范和協議
- 應用設計規范
- 開發人員協議
- 免責聲明