如何在 Chrome 中将 Google 云消息传递响应发送到 Service worker 的通知事件

How to get Google Cloud Messaging response into a Service worker's event for notifications in Chrome

正在接收消息,当我使用示例文本进行通知时确实弹出了通知。

我已经在 Google 上设置了一个帐户,用于在 Chrome 上推送通知,但响应似乎是空的。

服务人员

我在 service worker 上有这个,但它是空的。

self.addEventListener('push', function(event) {
  //console.log('Received a push message', event);
  console.log(event.data);
});

然后我用 Fetch 尝试了另一件事,使用本地主机 url。

var url = 'http://localhost/notification/index.php?type=fg';
self.addEventListener('push', function(event) {
  event.waitUntil(fetch(url).then(function(response) {
          console.log(response);
            return response.json();
      }).then(function(data) {
          console.log(data);
          //'data' does't have the json from the url
      })
    )
});

Chrome 尚不支持推送有效载荷,因此第一个代码段仅适用于 Firefox (https://serviceworke.rs/push-payload.html)。

您的第二种方法应该适用于任何浏览器 (https://serviceworke.rs/push-get-payload.html)。