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?