ServiceWorkerGlobalScope.onnotificationclick
Experimental: これは実験的な機能です。
本番で使用する前にブラウザー互換性一覧表をチェックしてください。
ServiceWorkerGlobalScope.onnotificationclick プロパティは、ServiceWorkerGlobalScope
オブジェクトで notificationclick
イベントが送り出されるたびに呼び出されるイベントハンドラーです。つまり、ユーザーが ServiceWorkerRegistration.showNotification()
で生成された通知をクリックしたときです。
メインスレッド、または Notification()
コンストラクターを使用して Service Worker ではない Worker で生成された通知は、Notification
オブジェクト自体で click
イベントを代わりに受け取ります。
ノート: Notification()
コンストラクターを使用して ServiceWorkerGlobalScope
内で通知を生成しようとすると、エラーがスローされます。
構文
ServiceWorkerGlobalScope.onnotificationclick = function(NotificationEvent) { ... };
例
self.onnotificationclick = function(event) {
console.log('On notification click: ', event.notification.tag);
event.notification.close();
// This looks to see if the current is already open and
// focuses if it is
event.waitUntil(clients.matchAll({
type: "window"
}).then(function(clientList) {
for (var i = 0; i < clientList.length; i++) {
var client = clientList[i];
if (client.url == '/' && 'focus' in client)
return client.focus();
}
if (clients.openWindow)
return clients.openWindow('/');
}));
};
仕様
仕様 | ステータス | コメント |
---|---|---|
Notifications API onnotificationclick の定義 |
現行の標準 | 初期定義。このプロパティは、ServiceWorkerGlobalScope の一部だが Notifications_API で定義されている。 |
ブラウザー実装状況
No compatibility data found for api.ServiceWorkerGlobalScope.onnotificationclick
.
Check for problems with this page or contribute missing data to mdn/browser-compat-data.