リッチなデスクトップ通知を使用して、重要なことが起こったことをユーザーに通知します。通知はブラウザ ウィンドウの外に表示されます。次のスナップショットに示すように、通知の外観と通知が表示される場所の詳細はプラットフォームによって異なります。
通知ウィンドウを作成するには、少量の JavaScript と、必要に応じて拡張機能内にパッケージ化された HTML ページを使用します。
例
まず、マニフェストで notifications
権限を宣言します。
{
"name": "My extension",
"manifest_version": 2,
...
"permissions": [
"notifications"
],
...
// Note: Because of bug 134315, you must declare any images you
// want to use with createNotification() as a web accessible resource.
"web_accessible_resources": [
"48.png"
],
}
次に、webkitNotifications
オブジェクトを使用して通知を作成します。
// Note: There's no need to call webkitNotifications.checkPermission().
// Extensions that declare the notifications permission are always
// allowed create notifications.
// Create a simple text notification:
var notification = webkitNotifications.createNotification(
'48.png', // icon url - can be relative
'Hello!', // notification title
'Lorem ipsum...' // notification body text
);
// Or create an HTML notification:
var notification = webkitNotifications.createHTMLNotification(
'notification.html' // html url - can be relative
);
// Then show the notification.
notification.show();
API リファレンス
デスクトップ通知のドラフト仕様をご覧ください。
他のビューとの通信
extension.getBackgroundPage と extension.getViews を使用して、通知と拡張機能の他のビューと通信できます。例:
chrome.extension.getBackgroundPage().doThing();
chrome.extension.getViews({type:"notification"}).forEach(function(win) {
win.doOtherThing();
});
例の表示数を増やす
通知の簡単な使用例が examples/api/notifications ディレクトリにあります。他の例とソースコードの確認方法については、サンプルをご覧ください。
html5rocks.com の通知のチュートリアルもご覧ください。権限関連のコードは無視してください。「通知」権限を宣言している場合は不要です。