python - Web 推送通知有效负载数据为空

标签 python push-notification service-worker payload web-push

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

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在 MDN 上。但根据 Push API 规范,任何有效负载都必须加密,否则浏览器将丢弃它并返回 null(请参阅 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 Push Payload Encryption 。以及原稿Message Encryption for Web Push .

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

关于python - Web 推送通知有效负载数据为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38992034/

相关文章:

ios - 需要指导在 Cordova 应用程序中使用 native APNs/GCM 为 iOS 推送通知

javascript - Serviceworker Bug event.respondWith

android - FCM 通知问题

python - 在 pandas 组内分配分位数

python - 我如何找到 Pyramid 中 URI 中的 anchor ?

python - 操作错误 : unable to open database file

android - 在android中实现推送通知的指南

javascript - JS Service Worker 在我不希望它运行的页面上运行

javascript - 在 Django 中使用 chrome 实现推送通知

python - python中一致的读写文件