Repository navigation
Push notifications - #242
Conversation
Aiq0
left a comment
There was a problem hiding this comment.
vyzera to dobre, vyzera ze to aj funguje, ale teda pozri komentare nizsie...
| push_user_ids = [ | ||
| user.id for user in users if NotificationChannel.PUSH in channels_map[user.id] | ||
| ] | ||
| if push_user_ids: | ||
| subscribed_ids = set( | ||
| PushSubscription.objects.filter(user_id__in=push_user_ids) | ||
| .values_list("user_id", flat=True) | ||
| .distinct() | ||
| ) | ||
| for user_id in subscribed_ids: | ||
| push_notification.delay( | ||
| user_id=user_id, | ||
| contest_id=contest.id, | ||
| title=title, | ||
| content=content, | ||
| link=link, | ||
| ) | ||
|
|
| return public, private | ||
|
|
||
| if not settings.DEBUG: | ||
| return "", "" |
| public, private = generate_vapid_keys() | ||
| _DEV_KEY_PATH.write_text(json.dumps({"public_key": public, "private_key": private})) | ||
| return public, private |
There was a problem hiding this comment.
trochu nerozumiem na co je .vapid.json, ak len preto, aby si si to mohol precitat, nechces to radsej vypisat na stdout?
There was a problem hiding this comment.
mam pocit ze toto nie je potrebne, ak si teda rebasnes tieto veci (teda tam som to dal to tej parent migracie)
potom by to ale teda chcelo fixnut tu dalsiu migraciu...
| class ServiceWorkerView(View): | ||
| def get(self, request: HttpRequest) -> FileResponse: | ||
| path = finders.find("push/sw.js") | ||
| if not path: | ||
| raise Http404 | ||
|
|
||
| response = FileResponse(open(path, "rb"), content_type="application/javascript") | ||
| response["Cache-Control"] = "no-cache" | ||
| response["Service-Worker-Allowed"] = "/" | ||
| return response |
There was a problem hiding this comment.
nestaci to len dat do rootu staticu a dobre bude?
ze returnut FileResponse je trochu hlupe, najma ked pred tym je Caddy, ktora to via poriesit sama ked ju pekne poprosis...
| # Web Push. Leave empty in development to generate seminare/.vapid.json. | ||
| VAPID_PUBLIC_KEY= | ||
| VAPID_PRIVATE_KEY= | ||
| VAPID_ADMIN_EMAIL=web@trojsten.sk |
There was a problem hiding this comment.
do env examplu by som kludne dal nejake fixne testovacie kluce, s komentarom, ze v produkcii zmen
There was a problem hiding this comment.
ked tak nad tym rozmyslam, mozno by nebolo zle cele notify dat ako @job, lebo tie .delay insertuju veci do db po jednom...
Receive notifications via web push API. Need to setup
In
.env- values can be generated viadef generate_vapid_keys() -> tuple[str, str]:inpush.py. Need to be constant in production, if not present they are generated automatically. Tested on firefox, notifications works there. Need to test in other browsers - at least chrome.