大网数据广告活动
Web 标准与前端开发

PushSubscription

· 52CE 我爱测

The `PushSubscription` interface of the Push API provides a subscription's URL endpoint along with the public key and secrets that should be used for encrypting push messages to this subscription. This information must be passed to the application server, using any desired applic

Push API

The PushSubscription interface of the Push API provides a subscription's URL endpoint along with the public key and secrets that should be used for encrypting push messages to this subscription. This information must be passed to the application server, using any desired application-specific method.

The interface also provides information about when the subscription will expire, and a method to unsubscribe from the subscription.

Instance properties

  • PushSubscription.endpoint
    • : A string containing the endpoint associated with the push subscription.
  • PushSubscription.expirationTime
    • : A DOMHighResTimeStamp of the subscription expiration time associated with the push subscription, if there is one, or null otherwise.
  • PushSubscription.options
    • : An object containing the options used to create the subscription.
  • PushSubscription.subscriptionId
    • : A string containing the subscription ID associated with the push subscription.

Instance methods

  • PushSubscription.getKey()
    • : Returns an ArrayBuffer which contains the client's public key, which can then be sent to a server and used in encrypting push message data.
  • PushSubscription.toJSON()
    • : Returns a JSON-serializable plain object representing the PushSubscription object. Automatically called by JSON.stringify().
  • PushSubscription.unsubscribe()
    • : Starts the asynchronous process of unsubscribing from the push service, returning a Promise that resolves to a boolean value when the current subscription is successfully unregistered.

Description

Each browser uses a particular push service. A service worker can use PushManager.subscribe() to subscribe to the supported service, and use the returned PushSubscription to discover the endpoint where push messages should be sent.

The PushSubscription is also used to get the public key and secret that the application server must use to encrypt the messages that it sends to the push service. Note that the private keys used to decrypt push messages are not shared by the browser, and are used to decrypt messages before they are passed to the service worker. This ensures that push messages remain private as they pass through the push server infrastructure.

The service worker doesn't need to know anything about the endpoints or encryption, other than to pass the relevant information onto the application server. Any mechanism may be used to share the information with the application server.

Example

Sending coding information to the server

The p256dh public key and auth secret used for encrypting the message are provided to the service worker via its push subscription, using the PushSubscription.getKey() method, along with the target endpoint for sending push messages in PushSubscription.endpoint. The coding that should be used for encryption is provided by the PushManager.supportedContentEncodings static property.

This example shows how you might put the needed information from PushSubscription and supportedContentEncodings into a JSON object, serialize it using JSON.stringify(), and post the result to the application server.

// 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.getKey("p256dh"),
    auth: pushSubscription.getKey("auth"),
  },
  encoding: PushManager.supportedContentEncodings,
  /* other app-specific data, such as user identity */
};

// Stringify the object and post to the app server
fetch("https://example.com/push/", {
  method: "post",
  body: JSON.stringify(subscriptionObject),
});

Unsubscribing from a push manager

navigator.serviceWorker.ready
  .then((reg) => reg.pushManager.getSubscription())
  .then((subscription) => subscription.unsubscribe())
  .then((successful) => {
    // You've successfully unsubscribed
  })
  .catch((e) => {
    // Unsubscribing failed
  });

Specifications

Browser compatibility

See also

来源:MDN Web Docs / Mozilla 贡献者 · 作者与贡献记录 · CC BY-SA 2.5。离线整理了版式、链接和媒体显示。

资料来源与适用范围

资料来源
MDN Web Docs / Mozilla 贡献者
适用范围
请按文中版本与环境说明核对
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 2.5

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。