Initial boilerplate for accepting subscriptions
This commit is contained in:
parent
a8492491b9
commit
764e9aaa3f
18
djangoldp_webpushnotification/templates/sw.js
Normal file
18
djangoldp_webpushnotification/templates/sw.js
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
// Register event listener for the 'push' event.
|
||||||
|
self.addEventListener("push", function (event) {
|
||||||
|
// Retrieve the textual payload from event.data (a PushMessageData object).
|
||||||
|
// Other formats are supported (ArrayBuffer, Blob, JSON), check out the documentation
|
||||||
|
// on https://developer.mozilla.org/en-US/docs/Web/API/PushMessageData.
|
||||||
|
const eventInfo = event.data.text();
|
||||||
|
const data = JSON.parse(eventInfo);
|
||||||
|
const head = data.head || "New Notification 🕺🕺";
|
||||||
|
const body = data.body || "This is default content. Your notification didn't have one 🙄🙄";
|
||||||
|
|
||||||
|
// Keep the service worker alive until the notification is created.
|
||||||
|
event.waitUntil(
|
||||||
|
self.registration.showNotification(head, {
|
||||||
|
body: body,
|
||||||
|
icon: "https://i.imgur.com/MZM3K5w.png",
|
||||||
|
})
|
||||||
|
);
|
||||||
|
});
|
15
djangoldp_webpushnotification/urls.py
Normal file
15
djangoldp_webpushnotification/urls.py
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
from django.urls import include, path
|
||||||
|
from django.views.generic import TemplateView
|
||||||
|
|
||||||
|
from .views import home, send_push
|
||||||
|
|
||||||
|
urlpatterns = [
|
||||||
|
path("send_push", send_push),
|
||||||
|
path("webpush/", include("webpush.urls")),
|
||||||
|
path(
|
||||||
|
"sw.js",
|
||||||
|
TemplateView.as_view(
|
||||||
|
template_name="sw.js", content_type="application/x-javascript"
|
||||||
|
),
|
||||||
|
),
|
||||||
|
]
|
29
djangoldp_webpushnotification/views.py
Normal file
29
djangoldp_webpushnotification/views.py
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
import json
|
||||||
|
|
||||||
|
from django.conf import settings
|
||||||
|
from django.http.response import HttpResponse, JsonResponse
|
||||||
|
from django.shortcuts import get_object_or_404, render
|
||||||
|
from django.views.decorators.csrf import csrf_exempt
|
||||||
|
from django.views.decorators.http import require_GET, require_POST
|
||||||
|
from djangoldp_account.models import LDPUser
|
||||||
|
from webpush import send_user_notification
|
||||||
|
|
||||||
|
|
||||||
|
@require_POST
|
||||||
|
@csrf_exempt
|
||||||
|
def send_push(request):
|
||||||
|
try:
|
||||||
|
body = request.body
|
||||||
|
data = json.loads(body)
|
||||||
|
|
||||||
|
if "head" not in data or "body" not in data or "id" not in data:
|
||||||
|
return JsonResponse(status=400, data={"message": "Invalid data format"})
|
||||||
|
|
||||||
|
user_id = data["id"]
|
||||||
|
user = get_object_or_404(LDPUser, pk=user_id)
|
||||||
|
payload = {"head": data["head"], "body": data["body"]}
|
||||||
|
send_user_notification(user=user, payload=payload, ttl=1000)
|
||||||
|
|
||||||
|
return JsonResponse(status=200, data={"message": "Web push successful"})
|
||||||
|
except TypeError:
|
||||||
|
return JsonResponse(status=500, data={"message": "An error occurred"})
|
Reference in New Issue
Block a user