I have been tinkering with django-push-notifications
, followed a google tutorial.
My current status is a web which seems to work and register and asks for notifications permissions and so on. I am running through ./manage.py runserver
and using localhost:8000
. The Service Worker is currently working (as expected?). When I perform a device.send_message("Hello world")
(through the push_notification
module) I can see that I receive an event
in the Javascript application. Currently testing in Chrome, I plan to tailor it for Android.
No matter what I try, the event.data
is always Null
. I have tried sending regular messages, structured messages, and the keyword argument extra
for the send_message
method with no luck.
I assume that I am missing something?
Snippets of what I am running (incomplete, but not sure what is relevant)
# Trigger push notifications
device = GCMDevice.objects.all()
device.send_message(<some data here>, extra=<some data here>)
And regarding the Javascript in the Service Worker:
self.addEventListener('push', function(event) {
console.log('Push message', event);
var data = {};
if (event.data) {
data = event.data.json();
}
console.log('Push data', data);
...
Regarding the set-up and registration:
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);
});
Which is the correct way of transfering information from Django to the notification?
It seems that the implementation of django-push-notifications does not support web push encryption, which is required in order to send information from the server to the client (according to google and also to mozilla).
That means that either I change to another implementation (like
django-webpush
as @Salva suggested), or I add support for that indjango-push-notifications
.I honestly think that that is the problem, but I am not completely sure.
Edit: Third option, roll my own using
pywebpush
. Given that I already had the JS stuff in place, it wasn't so hard. If I had to start over, maybe I would look closer todjango-webpush
.