javascript - 如何将 Google Cloud Messaging 响应放入 Service Worker 的事件中以获取 Chrome 中的通知

标签 javascript android google-chrome google-cloud-messaging service-worker

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

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

服务人员

我在 Service Worker 上有这个,但它是空的。

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

然后我还尝试使用 localhost url 进行 Fetch 的另一件事。

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 )。

关于javascript - 如何将 Google Cloud Messaging 响应放入 Service Worker 的事件中以获取 Chrome 中的通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35376981/

相关文章:

java - Highcharts 中的数组

javascript - 我的移动响应网站未正确调整大小

java - 带有内部嵌套 ScrollView 的 Recyclerview?

android - Koltin : Should one use coroutine with delay or CountDownTimer 上 Android 中的定时器实现

javascript - 在每个循环中使用 setTimeout 不起作用

javascript - 如何让两个框架一起拖动?(jQuery)

java - 将短数组转换为字节数组和将字节数组转换为短数组

java - 启用不跟踪

javascript - CSS 变换旋转仅适用于 Firefox

css - 为什么 Chrome 会从 style 属性中读取 svg 圆半径?