PushSubscription: getKey() メソッド

Baseline 2023
Newly available

Since March 2023, this feature works across the latest devices and browser versions. This feature might not work in older devices or browsers.

getKey()PushSubscription インターフェイスのメソッドで、サーバーに送信されてプッシュメッセージのデータを暗号化するために使用される、クライアントの公開鍵を表す ArrayBuffer を返します。

構文

js
getKey(name)

引数

method

クライアントの公開鍵の生成に使用される暗号化方式です。取りうる値は以下の通りです。

p256dh

P-256 曲線(つまり、NIST secp256r1 楕円曲線)の楕円曲線ディフィー・ヘルマン公開鍵です。結果として得られるキーは、ANSI X9.62 フォーマットで非圧縮ポイントとなります。

auth

Message Encryption for Web Push で記述されている認証秘密です。

返値

js
reg.pushManager.getSubscription().then((subscription) => {
  // プッシュメッセージからサブスクライブ / アンサブスクライブ
  // する任意の UI を有効にする

  subBtn.disabled = false;

  if (!subscription) {
    console.log("Not yet subscribed to Push");
    // プッシュを購読していない場合、ユーザーがプッシュできるように
    // UI を設定する。
    return;
  }

  // プッシュメッセージを購読していることを表示するように
  // UI を設定する。
  subBtn.textContent = "Unsubscribe from Push Messaging";
  isPushEnabled = true;

  // 購読ステータスのための UI 要素の設定を含んで状態を初期化し、
  // プッシュ経由でサブスクライバーを更新する。
  const endpoint = subscription.endpoint;
  const key = subscription.getKey("p256dh");
  const auth = subscription.getKey("auth");

  // ...
});

仕様書

Specification
Push API
# dom-pushsubscription-getkey

ブラウザーの互換性

Report problems with this compatibility data on GitHub
desktopmobile
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
getKey()

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support
No support
No support
See implementation notes.