我一直在摆弄django-push-notifications,关注了一个google tutorial。
我目前的状态是一个网站,它似乎可以工作和注册,并要求通知,权限等。我正在运行./manage.py runserver并使用localhost:8000。Service Worker当前正在工作(如预期的那样?)。当我执行一个device.send_message("Hello world") (通过push_notification模块)时,我可以看到我在Javascript应用程序中收到了一个event。目前在Chrome上测试,我计划为Android定制它。
无论我怎么尝试,event.data都是Null。我尝试过发送常规消息、结构化消息和send_message方法的关键字参数extra,但都没有成功。
我猜我错过了什么?
我正在运行的代码片段(不完整,但不确定哪些是相关的)
# Trigger push notifications
device = GCMDevice.objects.all()
device.send_message(<some data here>, extra=<some data here>)关于Service Worker中的Javascript:
self.addEventListener('push', function(event) {
console.log('Push message', event);
var data = {};
if (event.data) {
data = event.data.json();
}
console.log('Push data', data);
...关于设置和注册:
navigator.serviceWorker.register('/sw.js').then(function () {
return navigator.serviceWorker.ready;
}).then(function (serviceWorkerRegistration) {
return serviceWorkerRegistration.pushManager.subscribe({userVisibleOnly: true})
}).then(function (pushSubscription) {
// Last part of the URL is indeed the Registration ID for the GCM
var reg_id = pushSubscription.endpoint.split('/').pop();
console.log('Subscribed! Endpoint:', reg_id);
// Send the registration_id, which is the only required field
return $http({
method: "POST",
url: "{% url 'api:gcmdevice-list' %}",
data: {registration_id: reg_id}
});
}).catch(function (error) {
console.log('Service Worker Error:', error);
});将信息从Django传输到通知的正确方式是什么?
发布于 2016-10-25 16:50:33
似乎django推送通知的实现不支持网页推送加密,这是从服务器向客户端发送信息所必需的(根据google和到mozilla)。
这意味着要么我改成另一个实现(就像@Salva建议的那样使用django-webpush ),要么我在django-push-notifications中添加对它的支持。
老实说,我认为这就是问题所在,但我不完全确定。
编辑:第三个选项,使用pywebpush滚动我自己的。考虑到我已经准备好了JS的东西,所以这并不难。如果我必须重新开始,也许我会更接近django-webpush。
https://stackoverflow.com/questions/40206957
复制相似问题