ServiceWorkerRegistration.getNotifications()
Baseline
広く利用可能
この機能は広く実装されており、多くのバージョンの端末やブラウザーで動作します。2023年3月以降、すべてのブラウザーで利用可能です。
getNotifications() は ServiceWorkerRegistration インターフェイスのメソッドで、現在のサービスワーカー登録を介して現在のオリジンから作成された順序で通知のリストを返します。オリジンには、アクティブではあるがスコープが異なるサービスワーカー登録が多数あります。 同じオリジンの 1 つのサービスワーカーによって作成された通知は、同じオリジンの他のアクティブなサービスワーカーでは利用できません。
構文
js
getNotifications()
getNotifications(options)
引数
options省略可-
返される通知を絞り込むオプションを含むオブジェクト。 使用可能なオプションは次のとおりです。
tag-
通知タグを表す文字列。 指定した場合、このタグを持つ通知のみが返されます。
返値
Promise で、 Notification オブジェクトのリストに解決されます。
例
js
navigator.serviceWorker.register("sw.js");
const options = { tag: "user_alerts" };
navigator.serviceWorker.ready.then((registration) => {
registration.getNotifications(options).then((notifications) => {
// notifications で何かをします
});
});
仕様書
| 仕様書 |
|---|
| Notifications API> # dom-serviceworkerregistration-getnotifications> |