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 |
浏览器兼容性
加载中…