Web Push 通知 Payload 数据变空 [英] Web Push notification Payload data is getting null

查看:84
本文介绍了Web Push 通知 Payload 数据变空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为网络实现网络推送通知,我遵循以下示例,除了我使用 python 脚本连接到 gcm/fcm 并生成有效负载的服务器部分.我收到推送事件,但 event.data 为空.

Hi I'm trying to implement web push notification for web i followed the following example,except the server part for which i'm using python script to connect to gcm/fcm and generating payload .i'm getting the push event but the event.data coming null .

from datetime import datetime
from pyfcm import FCMNotification
pushService = FCMNotification(api_key ='xxx'}
registrationId=""
messageTitle = "New update available"
messageBody ="some message"
dryRun = False
extraData = {}
extraData['title'] = "nknkn"

sw.js

self.addEventListener('push',function(e){

console.log('Push Message Recevied',e.data);
var title = 'Push message';
e.waitUntil(
    self.registration.showNotification(title, {
        body: 'The Message form data',
        icon: 'icon.jpg',
        tag: 'my-tag'
    }));
});

推荐答案

Google Chrome 和 Mozilla Firefox 目前都支持推送消息的有效负载,请参阅 PushMessageData.但是根据 Push API 规范,任何有效负载都必须加密,否则浏览器将丢弃它并返回 null(请参阅 11.1.6):

Both, Google Chrome and Mozilla Firefox currently support payload for push messages, see PushMessageData on MDN. But according to the Push API specification, any payload MUST be encrypted, otherwise a browser will discard it and return null (see 11.1.6):

如果推送消息由于任何原因无法解密,或者它没有加密并且包含任何有效负载,则丢弃该消息并终止此过程.如果推送消息不包含任何内容,则推送消息可能为空,否则对于未使用与推送订阅关联的密钥对成功解密的推送消息,不得触发推送事件.

If the push message could not be decrypted for any reason, or if it is not encrypted and contains any payload, discard the message and terminate this process. A push message may be empty if it contains no content, but otherwise push event must not be fired for a push message that was not successfully decrypted using the key pair associated with the push subscription.

这是 Google Developers 的一篇好文章,其中详细解释了它:Web 推送有效负载加密.以及Web 推送消息加密的原始草案.

Here is a good article from Google Developers, which explains it with more details: Web Push Payload Encryption. And original draft of the Message Encryption for Web Push.

我还可以建议您查看已在不同语言上实现的 WebPush 库集:web-推送库.你也可以在那里找到一个用 Python 编写的库.还有另一个 Java 库,它可以向 Chrome 和 Firefox 发送带有负载的推送消息:https://github.com/MartijnDwars/web-push.

I also can suggest you to look at the set of already implemented libraries for WebPush on different languages: web-push-libs. You can find there a lib written on Python too. And another lib on Java, which can send push messages with a payload to Chrome and Firefox: https://github.com/MartijnDwars/web-push.

这篇关于Web Push 通知 Payload 数据变空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆