带负载的 Chrome 网络推送通知

Chorme Web Push Notification with payload

self.addEventListener('push', function(event) {
  var jsonObj = event.data.json();
}

我已经写了 chrome 网络推送通知,但是 event.data 总是空的。

目前我正在使用 GCMPushMessage 库,我想我需要 php curl 来发送通知。谁能帮我发送带有有效载荷的网络推送通知?

自 2015 年起,此 声明,这是预期的行为。

A downside to the current implementation of the Push API in Chrome is that you can't send any data with a push message. Nope, nothing. The reason for this is that in a future implementation, payload data 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 message. 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 to get information needed to populate a notification.

根据文档的规定,您必须获取信息,然后将其填充到通知中。

2016 年 3 月发布了一个名为:Web Push Payload Encryption

的更新

Prior to Chrome 50, push messages could not contain any payload data. When the 'push' event fired in your service worker, all you knew was that the server was trying to tell you something, but not what it might be. You then had to make a follow up request to the server and obtain the details of the notification to show, which might fail in poor network conditions.

Now in Chrome 50 (and in the current version of Firefox on desktop) you can send some arbitrary data along with the push so that the client can avoid making the extra request. However, with great power comes great responsibility, so all payload data must be encrypted.

确保您已遵循在 Chrome 的网络推送中应用负载所需的实施。像一些客户端更改和服务器端更改。

希望这对您有所帮助。