JavaScript Desktop/Push Notification
Hello, i have a javascript that sends a desktop notification whenever i upload a picture on my website. But if someone else uploads a picture while i'm not on the website, i dont get a notification. ChatGPT told me i need a service worker that runs in the background. But i just have no idea at all how to do that. So i would really appreciate it if someone could help me out.
9 Replies
This is the code i currently have
bump, still need help
Hey this doesn't fix your issue but don't use
var
, use let
or const
. var
is old syntaxThanks, but yeah, i know this, i will fix it. But this code was generated by ChatGPT and you probably already know that ChatGPT loves to use var instead of let or const 😂
ChatGPT loves to give wrong answers, so that’s probably why.
I suggest reading the documentation:
https://developer.mozilla.org/en-US/docs/Web/API/Notification
Well, the notification system is working just fine. it's just that it cannot run in the background. and for this i need a service worker. I have also noticed that all websites with push notifications have a js file usually called sw.js and this is for the service worker. so yeah, it really seems like this is required to make javascript run in the background
Yeah, main JS files don’t run when the tab is in the background. Service Workers aim to do that background stuff so that’d be the place to look.
Yeah. but it seems to be incredibly hard to make it or something, because i have asked in 3 different servers now and this is the only server where anyone have even replied :/
and everyone i asked about it has no idea how to do it
I've not done anything with service workers yet, but this series might be able to help some:
https://gomakethings.com/series/service-workers/
Okay thanks ! But yeah i am terrible at JavaScript, i only focus on CSS. but i will read it anyway, it cannot do any harm 🙂 and who knows, i may learn something new useful thing 😁 👍