PushManager: getSubscription() メソッド
Baseline 2023Newly available
Since March 2023, this feature works across the latest devices and browser versions. This feature might not work in older devices or browsers.
PushManager.getSubscription()
は PushManager
インターフェイスのメソッドで、既存のプッシュサブスクリプションを取得します。
これは既存のプッシュサブスクリプションの詳細を含む PushSubscription
で解決する Promise
を返します。既存のプッシュサブスクリプションが存在しない場合 null
値で解決します。
構文
js
getSubscription()
引数
なし。
返値
Promise
です。これは PushSubscription
オブジェクトまたは null
で解決されます。
例
このコードスニペットは、 push messaging and notification sample から取得したものです(使用できるライブデモはありません)。
js
// サブスクリプションをチェックするためにサービスワーカーの登録が必要
navigator.serviceWorker.ready.then((serviceWorkerRegistration) => {
// 既にプッシュメッセージのサブスクリプションがあるか?
serviceWorkerRegistration.pushManager
.getSubscription()
.then((subscription) => {
// プッシュメッセージに加入/脱退する任意の UI を有効にする
const pushButton = document.querySelector(".js-push-button");
pushButton.disabled = false;
if (!subscription) {
// プッシュに参加していない場合、ユーザーがプッシュを
// 有効化できるように UI を設定
return;
}
// 最新の subscriptionId でサーバー同期を維持
sendSubscriptionToServer(subscription);
showCurlCommand(subscription);
// プッシュメッセージに参加していることを表示するために
// UI を設定
pushButton.textContent = "Disable Push Messages";
isPushEnabled = true;
})
.catch((err) => {
console.error(`Error during getSubscription(): ${err}`);
});
});
仕様書
Specification |
---|
Push API # dom-pushmanager-getsubscription |
ブラウザーの互換性
Report problems with this compatibility data on GitHubdesktop | mobile | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
getSubscription |
Legend
Tip: you can click/tap on a cell for more information.
- Full support
- Full support
- No support
- No support
- See implementation notes.
The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.