我已经使用Firebase设置了推送通知,并且无法发送自定义数据。
我也尝试过PyFCM库,但是数据仍然为空。根据我发现的情况,payload requires encryption但我不确定如何实现。
如何在python中加密推送数据或通过推送通知发送自定义数据?
我希望将event_handler.py作为服务运行,轮询MySQL数据库以获取新通知,并向该用户发送推送通知。
我已经保存了用户的registration_id,我只需要能够通过推送通知发送数据。
service-worker.js
self.addEventListener('push', function(event) {
if (event.data) {
console.log(event.data.text());
}
console.log('Received a push message', event);
if (event.title) {
var title = event.title;
} else {
var title = 'New message.';
}
var body = 'You have received a push message.';
var icon = 'img/logo.png';
var tag = 'simple-push-demo-notification-tag';
event.waitUntil(
self.registration.showNotification(title, {
body: body,
icon: icon,
tag: tag,
})
);
});
我可以使用以下方式发送推送通知:
notification_handler.py
import requests
headers = {
'Content-type': 'application/json',
'Authorization': 'key=AAAAl...',
}
data="""{
"to" : "fk1jL...",
"data" : {
"title" : "hi there",
"body" : "new push message",
},
}"""
response = requests.post('https://fcm.googleapis.com/fcm/send', headers=headers, data = data)
但是,当接收到推送通知时,控制台输出中的数据为空:
Received a push message PushEvent {isTrusted: true, data: null, type: "push", target: ServiceWorkerGlobalScope, currentTarget: ServiceWorkerGlobalScope, …} bubbles: false cancelBubble: false cancelable: false composed: false currentTarget: ServiceWorkerGlobalScope {clients: Clients, registration: ServiceWorkerRegistration, onactivate: null, onfetch: null, oninstall: null, …} data: null defaultPrevented: false eventPhase: 0 isTrusted: true path: [] returnValue: true srcElement: ServiceWorkerGlobalScope {clients: Clients, registration: ServiceWorkerRegistration, onactivate: null, onfetch: null, oninstall: null, …} target: ServiceWorkerGlobalScope {clients: Clients, registration: ServiceWorkerRegistration, onactivate: null, onfetch: null, oninstall: null, …} timeStamp: 0 type: "push"
__proto__: PushEvent