Answer the question
In order to leave comments, you need to log in
3 push notifications come at once, what could be the matter?
Using the django addon webpush-django , I call the following function once:
def send_push(data):
try:
user_id = data['id']
user = get_object_or_404(User, pk=user_id)
payload = {
'head': data['head'],
'body': data['body'],
'url': data['url'],
}
send_user_notification(user=user, payload=payload, ttl=1800)
return JsonResponse(status=200, data={'message': 'Web push successful'})
except TypeError:
return JsonResponse(status=500, data={'message': 'An error occurred'})
self.addEventListener('push', function (event) {
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 ';
const url = data.url || '/';
event.waitUntil(
self.registration.showNotification(head, {
body: body,
icon: '/static/img/tf.jpg',
url: url,
})
);
});
Answer the question
In order to leave comments, you need to log in
Well, firstly, try except is not particularly appropriate here. get_object_or_404 will most likely return a non-json response and is also not appropriate here. Why is it impossible to immediately transfer data to payload? And send_user_notification will already parse the data and return the result, on the basis of which the response will be generated.
3 notifications can come for 2 reasons: the response from the server comes 3 times, or the event on the front fires 3 times. Look in the browser's network debugger, place print() in the view, see where you have looping there, check how many times the 'push' event fires on the front.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question