【问题标题】:Service Worker Window Clients Issue on Firefox - Web Push NotificationFirefox 上的 Service Worker 窗口客户端问题 - Web 推送通知
【发布时间】:2023-03-29 11:38:02
【问题描述】:

我正在尝试实现网络推送通知。当尝试检查天气网络应用程序是否打开时,Firefox 向服务人员抛出一个奇怪的错误。它在 chrome 上运行良好。

“Service Worker 事件 waitUntil() 被传递了一个被拒绝并以 'NotSupportedError: Operation is not supported' 的承诺。”

如果我将“includeUncontrolled”设置为 false,firefox 和 chrome 都会返回“windowClients”一个空白数组。未检测到页面。

这是我的推送事件处理程序,“clients.matchAll”是第 50 行。

// Push Notification Event Handler
self.addEventListener('push', function(event) {

  // Push Received
  event.waitUntil(

      // Check app page open
      self.clients.matchAll({ // Line 50
        includeUncontrolled: true, // Error occuring when enabling this
        type: 'window'
      })
      .then(function(windowClients) {

        // If no page instances show notification
        if (!windowClients.length) {

          // Get subscription key to call api
          return self.registration
              .pushManager
              .getSubscription()
              .then(function(subscription) {
                if (subscription) {

                  // Get push message data
                  var token = encodeURIComponent(String(subscription.endpoint).split('/').pop());
                  var url = 'api/push/data?token=' + token + '&type=' + getPushDeviceType();
                  return self.fetch(url, {credentials: 'include'})
                      .then(function(response) {

                        if (response.status === 200) {
                          return response.json()
                              .then(function(data) {
                                if (data) {

                                  // Display notification
                                  return self.registration
                                      .showNotification('App Notifications', {
                                        'body': data.msg,
                                        'icon': data.img,
                                        'tag': 'app'
                                      });
                                } else {
                                  return;
                                }
                              });
                        } else {
                          return;
                        }
                      });
                } else {
                  return;
                }
              });
        } else {
          return;
        }
      })
      );
});

萤火虫截图

【问题讨论】:

  • 这可以在 Chrome 中使用吗?
  • 顺便说一句,您可以将所有这些} else { return; } 转换为提前返回:if (!condition) { return; } 以降低缩进级别并提高代码可读性。
  • @Salva。是的,它在 chrome 中工作。谢谢:)

标签: javascript firefox service-worker web-push


【解决方案1】:

【讨论】:

    猜你喜欢
    • 2015-08-20
    • 1970-01-01
    • 2016-07-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-29
    相关资源
    最近更新 更多