PushManager: supportedContentEncodings 靜態屬性
注意:此功能在 Web Workers 中可用。
PushManager 介面的只讀靜態屬性 supportedContentEncodings 返回一個支援的內容編碼陣列,這些編碼可用於加密推送訊息的載荷。
使用者代理必須支援 RFC 8291 中定義的 aes128gcm 內容編碼,並且可能還支援規範先前版本中定義的內容編碼。返回的陣列是凍結的,接收方不得修改。
應用程式伺服器需要此編碼才能加密推送訊息以傳送到推送伺服器。用於加密的編碼也由應用伺服器包含在每條推送訊息的 Content-Encoding HTTP 標頭欄位中。
規範未定義客戶端程式碼應如何將支援的編碼傳送給應用程式伺服器,或者 PushSubscription 中的資訊,而這些資訊對於加密和傳送推送訊息也是必需的。一種方法如下面的示例部分所示。
值
一個字串陣列。這通常只包含一個值:"aes128gcm"。
異常
TypeError-
嘗試設定返回陣列中的值時會丟擲此錯誤。
示例
將編碼資訊傳送到伺服器
推送訊息在應用程式伺服器上加密,以便傳送到推送伺服器,並在傳遞給應用程式服務工作執行緒之前由瀏覽器解密。使用的公鑰和私鑰由瀏覽器生成,只有公鑰和關聯的金鑰才與 Web 應用共享,進而與應用程式伺服器共享。這確保了推送訊息在透過推送伺服器基礎結構傳輸時保持私密。
用於加密訊息的 p256dh 公鑰和 auth 金鑰透過推送訂閱提供給服務工作執行緒,使用 PushSubscription.getKey() 方法,以及用於將推送訊息傳送到目標終結點(在 PushSubscription.endpoint 中)。應使用 supportedContentEncodings 中提供的編碼進行加密。
此資訊可以透過任何機制傳送到應用程式伺服器。一種方法是將 PushSubscription 和 supportedContentEncodings 中的所需資訊放入 JSON 物件中,使用 JSON.stringify() 進行序列化,並將結果釋出到應用程式伺服器。
// Get a PushSubscription object
const pushSubscription =
await serviceWorkerRegistration.pushManager.subscribe();
// Create an object containing the information needed by the app server
const subscriptionObject = {
endpoint: pushSubscription.endpoint,
keys: {
p256dh: pushSubscription.getKeys("p256dh"),
auth: pushSubscription.getKeys("auth"),
},
encoding: PushManager.supportedContentEncodings,
/* other app-specific data, such as user identity */
};
// Stringify the object an post to the app server
fetch("https://example.com/push/", {
method: "POST",
body: JSON.stringify(pushSubscription),
});
規範
| 規範 |
|---|
| 推送 API # dom-pushmanager-supportedcontentencodings |
瀏覽器相容性
載入中…