javascript - 使用 Javascript 将数据/有效负载发送到 Google Chrome 推送通知

标签 javascript google-chrome push-notification

我正在处理 Google Chrome 推送通知,并且正在尝试将有效负载发送给 Google Chrome Worker,但是我不知道如何接收此有效负载。

我有一个 API 可以在数据库中创建和保存通知,我需要通过 https://android.googleapis.com/gcm/send 发送值并在工作线程上接收。 js

这是我的worker.js

    self.addEventListener('push', function(event) {
      var title = 'Yay a message.';
      var body = 'We have received a push message.';
      var icon = '/images/icon-192x192.png';
      var tag = 'simple-push-demo-notification-tag';

      event.waitUntil(
        self.registration.showNotification(title, {
          body: body,
          icon: icon,
          tag: tag
        })
      );
    });

这就是我对 GCM 的称呼

curl --header "Authorization: key=AIzaSyDQjYDxeS9MM0LcJm3oR6B7MU7Ad2x2Vqc" --header  "Content-Type: application/json" https://android.googleapis.com/gcm/send -d "{ \"data\":{\"foo\":\"bar\"}, \"registration_ids\":[\"APA91bGqJpCmyCnSHLjY6STaBQEumz3eFY9r-2CHTtbsUMzBttq0crU3nEXzzU9TxNpsYeFmjA27urSaszKtA0WWC3yez1hhneLjbwJqlRdc_Yj1EiqLHluVwHB6V4FNdXdKb_gc_-7rbkYkypI3MtHpEaJbWsj6M5Pgs4nKqQ2R-WNho82mnRU\"]}"

我尝试获取event.data,但是这是未定义的。

有人有什么想法或建议吗?

最佳答案

不幸的是,它看起来像 intended behavior :

A downside to the current implementation of the Push API in Chrome is that you can’t send a payload with a push message. Nope, nothing. The reason for this is that in a future implementation, payload will have to be encrypted on your server before it’s sent to a push messaging endpoint. This way the endpoint, whatever push provider it is, will not be able to easily view the content of the push payload. This also protects against other vulnerabilities like poor validation of HTTPS certificates and man-in-the-middle attacks between your server and the push provider. However, this encryption isn’t supported yet, so in the meantime you’ll need to perform a fetch request to get information needed to populate a notification.

如上所述,解决方法是在收到推送后联系后端并获取第 3 方服务器上存储的数据。

关于javascript - 使用 Javascript 将数据/有效负载发送到 Google Chrome 推送通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30335749/

相关文章:

javascript - Chrome Canary 29 中的弹出窗口?

google-chrome - 使用 Chrome 多功能框进行快速翻译

html - Chrome 将 div 移动 1,具体取决于窗口大小是奇数还是偶数 # of px

android - 收到推送通知后打开应用程序

ios - 应用更新后重新显示推送通知弹出窗口

javascript - ExtJS 5.1 - 在厨房水槽中实现中心布局示例?

javascript - Firebase:数据库引用 'on' 方法未运行回调 (javascript)

javascript - FBQ(Facebook 事件)与 Angular 4

css - Chrome 中 div 之间的 1px 白色间距

ios - 如何在 ios 中重新注册推送通知