# 推送订阅的过期与刷新

> 订阅过期时间意味着什么、浏览器刷新或丢失订阅时 pushsubscriptionchange 携带什么，以及哪些 Firefox 版本缺少它的两个成员。

`pushsubscriptionchange` 事件让 Service Worker 处理推送订阅的变化。订阅可能到期，由浏览器停用；浏览器或推送服务也可以随时替换订阅。事件携带旧订阅及替代它的新订阅。

## 支持情况

BCD `api.PushSubscription.expirationTime` 记录 Chrome 60、Firefox 96、Safari 16 与 iOS Safari 16.4 起支持，并镜像到该记录列出的 Chromium 系浏览器；Android WebView 无支持。MDN 把该属性标记为 Baseline“Widely available”，自 2023 年 3 月起在各浏览器中可用，且限于安全上下文。

MDN 把 `pushsubscriptionchange` 标记为“Limited availability”，并写明它“is not Baseline because it does not work in some of the most widely-used browsers”。BCD `api.ServiceWorkerGlobalScope.pushsubscriptionchange_event` 记录它自 Chrome 138 起支持；自 Firefox 44 与 Firefox for Android 48 起为部分实现；自 Safari 16 起支持，并附注通知在 macOS Ventura 及更高版本上受支持。它记录 iOS Safari 无支持。事件的两个成员在 [api/PushSubscriptionChangeEvent.json](https://github.com/mdn/browser-compat-data/blob/main/api/PushSubscriptionChangeEvent.json)（github.com）中单独记录，其中 `oldSubscription` 与 `newSubscription` 自 Chrome 138、Firefox 137、Safari 16 与 iOS Safari 16.4 起支持。

## 工作原理

[Push API](https://www.w3.org/TR/push-api/#subscription-refreshes)（w3.org）写明推送订阅“MAY have an associated subscription expiration time”，并且设置了过期时间后，它是“the time, in milliseconds since 00:00:00 UTC on 1 January 1970, at which the subscription will be deactivated”。同一段还要求用户代理“SHOULD attempt to refresh the push subscription before the subscription expires”，因此浏览器应在截止时间之前刷新，应用不需要自行安排这次刷新。

用 `PushSubscription.expirationTime` 可以读取过期时间。规范将它声明为 `readonly attribute EpochTimeStamp?`，有过期时间就返回该时间，没有就返回 `null`。`toJSON()` 把这个值复制到 `PushSubscriptionJSON` 字典，其中 `expirationTime` 默认为 `null`。MDN 的 [expirationTime](https://developer.mozilla.org/en-US/docs/Web/API/PushSubscription/expirationTime) 页面（developer.mozilla.org）把同一个值描述为一个 `DOMHighResTimeStamp` 或 `null`。

刷新不必等到过期。规范允许用户代理或推送服务“at any time, for example because it has reached a certain age”刷新任何关联 Service Worker 注册非 null 的订阅，并要求在用户代理因任何原因必须更换订阅密钥、且该注册非 null 时执行刷新。刷新时用原来的 `PushSubscriptionOptions` 创建新订阅，并保留 scope。新订阅必须使用不同的密钥对。随后用户代理触发 `pushsubscriptionchange`，以最初的订阅作为 `oldSubscription`、新的订阅作为 `newSubscription`。

规范为这次交接留出了宽限期：用户代理“MAY continue to accept messages for an old push subscription for a brief time after a refresh”，而一旦已经为刷新后的订阅收到消息，就必须停用旧订阅。停用会删除用户代理与推送服务两边存储的副本、停止投递后续消息，并禁止把该端点重新用于新的订阅。

`newSubscription` 为 `null` 是该事件的另一种形态。当订阅已无法再使用（例如因为它已过期）而用户代理又无法刷新它时，规范要求触发该事件，以正在停用的订阅作为 `oldSubscription`、`null` 作为 `newSubscription`。撤销某项权限后，用户代理可以以同样的方式触发该事件，并且必须停用受影响的订阅。`PushSubscriptionChangeEventInit` 的定义给出了该成员在这些路径上的通用含义：`newSubscription`“will be null when no new push subscription could be established, for example because the web application has lost express permission”。对应的另一面也有定义：`oldSubscription`“will be null when the user agent is not able to provide the full set of details, for example because of partial database corruption”。

规范将 `PushSubscriptionChangeEvent` 声明为 `[Exposed=ServiceWorker, SecureContext]` 并继承 `ExtendableEvent`，所以处理器可以使用 `waitUntil()`；`ServiceWorkerGlobalScope` 还暴露了一个 `onpushsubscriptionchange` 处理器属性。MDN 补充说该事件不可取消，也不冒泡。

## 示例

处理器在 Service Worker 中运行，没有页面可以接收结果。需要保存订阅变化，并在与服务器交换信息失败时重试。

### 重新订阅并上报新端点

把旧订阅的 `options` 原样传给 `subscribe()`。浏览器发起刷新时也会复用这个字典，其中保留了原来的 `applicationServerKey`。用 `waitUntil()` 包住整个操作，避免 worker 中途终止。

```js
self.addEventListener('pushsubscriptionchange', (event) => {
  event.waitUntil(
    (async () => {
      const options = event.oldSubscription?.options ?? { userVisibleOnly: true };
      const next = await self.registration.pushManager.subscribe(options);
      await fetch('/push/rotate', {
        method: 'POST',
        headers: { 'Content-type': 'application/json' },
        body: JSON.stringify({
          old: event.oldSubscription?.endpoint ?? null,
          new: next.toJSON(),
        }),
      });
    })(),
  );
});
```

规范定义 `toJSON()` 返回 `endpoint`、`expirationTime` 以及 base64url 形式的 `p256dh` 与 `auth` 密钥，应用服务器用这些信息加密下一条消息。

### 处理缺少这两个成员的浏览器

BCD 记录 Firefox 44 与 Firefox for Android 48 为部分实现，其附注写明该事件“does not have the `oldSubscription` and `newSubscription` properties”，而这两个成员自 Firefox 137 起具备。在那些版本上读取 `event.oldSubscription` 的处理器没有可比对的对象，所以先检测该成员，并回退到注册对象当前的订阅。

```js
self.addEventListener('pushsubscriptionchange', (event) => {
  event.waitUntil(
    (async () => {
      if (!('oldSubscription' in event)) {
        // Firefox 137 之前：事件触发时两个成员都没有。
        const current = await self.registration.pushManager.getSubscription();
        await fetch('/push/resync', {
          method: 'POST',
          body: JSON.stringify({ current: current ? current.toJSON() : null }),
        });
        return;
      }
      await rotate(event.oldSubscription, event.newSubscription);
    })(),
  );
});
```

处理器拿不到旧端点，`/push/resync` 路由必须用其他信息匹配已有记录。用稳定的安装标识符关联推送记录，仅靠端点无法完成匹配。

### 检测该事件，并在它不存在时对账

BCD 记录 iOS Safari 不支持 `pushsubscriptionchange`。在那个环境里为它注册的处理器收不到事件，所以上面的刷新与停用路径必须用别的方式覆盖。`ServiceWorkerGlobalScope` 暴露了一个 `onpushsubscriptionchange` 处理器属性，这让 `'onpushsubscriptionchange' in self` 成为 worker 在依赖该事件触发之前可以做的一次检测。检测不通过时，改为把 `getSubscription()` 与服务端记录做比对：`activate` 事件给了 worker 一个在它安装的每个版本上执行这件事的时机。

```js
if ('onpushsubscriptionchange' in self) {
  self.addEventListener('pushsubscriptionchange', (event) => {
    event.waitUntil(rotate(event.oldSubscription, event.newSubscription));
  });
} else {
  // 该 worker 中没有 pushsubscriptionchange：改为在激活时对账。
  self.addEventListener('activate', (event) => {
    event.waitUntil(reconcileSubscription());
  });
}

async function reconcileSubscription() {
  const current = await self.registration.pushManager.getSubscription();
  await fetch('/push/reconcile', {
    method: 'POST',
    headers: { 'Content-type': 'application/json' },
    body: JSON.stringify({
      installationId: await readInstallationId(), // 记录所依据的那个稳定索引键
      current: current ? current.toJSON() : null, // null：没有可投递的目标
    }),
  });
}
```

订阅停用后不再接收消息，其端点也不能用于新订阅。规范没有定义应用服务器如何获知停用，因此不能从 Push API 规范推断某个投递响应就表示订阅已停用。无论浏览器是否支持这个事件，都可以用示例中的对账调用同步记录。

### 在页面上读取过期时间

`expirationTime` 在页面与 worker 中都可读，这让客户端可以把截止时间与端点一并发给服务器。把 `null` 当作“没有记录截止时间”，这正是订阅没有过期时间时取值器返回的结果。

```js
async function describeSubscription(registration) {
  const subscription = await registration.pushManager.getSubscription();
  if (!subscription) return { subscribed: false };
  const { endpoint, expirationTime } = subscription;
  return {
    subscribed: true,
    endpoint,
    expiresAt: expirationTime === null ? null : new Date(expirationTime).toISOString(),
  };
}
```

服务器记录截止时间后，就能停止向已知即将停用的端点发消息。按规范，订阅停用后不再接收消息，端点也不能复用。

## 实测行为

BCD 在 `api.ServiceWorkerGlobalScope.pushsubscriptionchange_event` 上给 Firefox 的附注写道“The event does not have the `oldSubscription` and `newSubscription` properties. See bug 1497429.”（[api/ServiceWorkerGlobalScope.json](https://github.com/mdn/browser-compat-data/blob/main/api/ServiceWorkerGlobalScope.json)，github.com），而 `api.PushSubscriptionChangeEvent` 上对应 Firefox 137 的附注写道“Before Firefox 137, The `pushsubscriptionchange` event is fired, but does not have the `oldSubscription` and `newSubscription` properties. See bug 1635524.”

MDN 的事件页面带有一条附注，写道“In earlier drafts of the specification, this event was defined to be sent when a `PushSubscription` has expired”（[pushsubscriptionchange event](https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorkerGlobalScope/pushsubscriptionchange_event)，developer.mozilla.org）。已发布的规范给出了更宽的范围：该事件“indicates a change in a push subscription that was triggered outside of the application's control, for example because it has been refreshed, revoked or lost”。

:::caution
规范在触发该事件处的注释建议，在把新推送订阅的详情发送给应用服务器时考虑“a more reliable synchronization mechanism such as [WEB-BACKGROUND-SYNC]”，因为“the user might be subject to unreliable network conditions that could cause a fetch to fail”。MDN 在其事件页面重复了这一点，指出 `fetch()` 在应用离线时不会工作，并要求作者改用其他方式同步订阅信息，或者处理好交换失败的情况。
:::

## 另请参阅

- [Push API: subscription refreshes](https://www.w3.org/TR/push-api/#subscription-refreshes)（w3.org）
- [ServiceWorkerGlobalScope: pushsubscriptionchange event](https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorkerGlobalScope/pushsubscriptionchange_event)（developer.mozilla.org）
- [Web Push 与 PushManager.subscribe()](/zh/reference/notifications/web-push/)
- [iOS 与 iPadOS 上的 Web Push](/zh/reference/notifications/ios-safari-push/)
- [Notification.requestPermission()](/zh/reference/notifications/permissions/)