【问题标题】:pushManager.subscribe() randomly fails with AbortErrorpushManager.subscribe() 随机失败并出现 AbortError
【发布时间】:2021-12-29 04:55:24
【问题描述】:

这发生在仅限 Chrome(应用程序目前不支持任何其他浏览器)和最新版本上。

我无法重现此问题,但它会定期出现在我们随机用户的监控仪表板中。

错误信息:“AbortError: 注册失败 - 推送服务错误”

以下代码块在页面加载时运行:

const currentNotificationsPermission = await navigator.permissions.query({ name: 'notifications' });
currentNotificationsPermission.onchange = function onchange() {
  // Update the internal state, this can happen after user interaction (clicking a "Allow notifications" button)
};

这在页面加载时运行(当内部状态为 granted 时),或者在用户手势后内部状态 更改granted 时:

navigator.serviceWorker.register('/serviceworker.js', { scope: '/myScope' });
navigator.serviceWorker.ready.then((registration) => {
  const subscribeOptions = {
    userVisibleOnly: true,
    applicationServerKey: 'myKey',
  };

  return registration.pushManager.subscribe(subscribeOptions);
}).then((pushSubscription) => {
  // Send pushSubscription to server, doesn't run in case of AbortError
}).catch((err) => { 
  console.log(err); // AbortError, randomly
});

【问题讨论】:

    标签: javascript service-worker web-push


    【解决方案1】:

    如果你不能重现问题,我猜你不能检查chrome://gcm-internals/

    一种可能性是您的某些用户可能具有阻止使用 GCM 的 Chrome 设置或加载项,但错误消息与此不完全一致。

    我的猜测是这是TOO_MANY_REGISTRATIONS 的情况,其中设备在 FCM 中的个人注册过多。可能是您的某些用户的客户端应用程序过多,或者他们的某个客户端应用程序可能会一遍又一遍地自行注册,从而导致注册次数过多。

    【讨论】:

      【解决方案2】:

      根据 Push API 规范,subscribe 方法可能以几种方式发生此错误:https://w3c.github.io/push-api/#dom-pushmanager

      1. 如果检索与 Service Worker(返回 AbortError)。
      2. 如果由于运行 create a push subscription 步骤给定选项而导致设置订阅出错(返回 AbortError)。

      这些都是浏览器的内部进程,因为它们涉及与内部推送服务的交互。

      如此处所述,推送服务不适用于开发人员:https://developers.google.com/web/fundamentals/push-notifications/how-push-works#who_and_what_is_the_push_service

      由于 Chrome 的这项服务,出现了一些历史错误。早在 Chrome 59 时,我就可以看到一些 posts

      现在,您可能希望使用 try/catch 保护这行代码,并记录您可以访问它们的详细信息 registration.pushManager.subscribe(subscribeOptions);

      如果失败或由于 Chrome 的旧版本而以某种方式遇到问题,它可能只需要重试。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-04-24
        • 2013-04-10
        • 2017-07-27
        • 1970-01-01
        • 2019-05-08
        • 1970-01-01
        相关资源
        最近更新 更多