0

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?

MariusSiuram
  • 3,380
  • 1
  • 21
  • 40
  • Have you checked `event.data` is `null`? Perhaps the call to `.json()` is returning `null`? Can you try doing `event.date.text()` instead? – Salva Oct 24 '16 at 16:05
  • From the Chrome console I can see that `event.data` is `null`, from the beginning. – MariusSiuram Oct 24 '16 at 16:20
  • 1
    I think there is something wrong with the implementation, can you try [django-webpush](https://github.com/safwanrahman/django-webpush) instead? – Salva Oct 25 '16 at 07:55
  • @Salva Thanks for the suggestion, I detected some differences regarding `p256dh`, which drove me to understand what they mean. However, at the moment I am unsuccessful with `django-webpush` to work (some `AppRegistryNotReady` exception) and it doesn't quite fit my data model right now. I will keep trying. – MariusSiuram Oct 25 '16 at 08:52

1 Answers1

1

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 in django-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 to django-webpush.

MariusSiuram
  • 3,380
  • 1
  • 21
  • 40