PushSubscription: toJSON() 方法

Baseline 已廣泛支援

此功能已成熟,並可在許多裝置和瀏覽器版本上執行。自 2023 年 3 月以來,它已在各種瀏覽器中可用。

安全上下文: 此功能僅在安全上下文(HTTPS)中可用,且支援此功能的瀏覽器數量有限。

注意:此功能在 Web Workers 中可用。

PushSubscription 介面的 toJSON() 方法是一個標準序列化程式:它返回訂閱屬性的 JSON 表示,提供了一個有用的快捷方式。

語法

js
toJSON()

引數

無。

返回值

一個 JSON 物件。它包含訂閱端點、expirationTime 和公鑰,分別作為 endpoint 成員、expirationTime 成員和 keys 成員。

示例

js
navigator.serviceWorker.ready.then((reg) => {
  reg.pushManager.getSubscription().then((subscription) => {
    const mySubscription = subscription.toJSON();
    // do something with subscription details
  });
});

規範

規範
推送 API
# dom-pushsubscription-tojson

瀏覽器相容性