I've been following a tutorial on implementing push notifications in VueJS from this link.
After running the register function on the created hook, I encountered an issue:
register() {
if ("serviceWorker" in navigator && "PushManager" in window) {
console.log("Service Worker and Push is supported");
navigator.serviceWorker
.register("/sw.js")
.then(function (swReg) {
console.log("Service Worker is registered", swReg);
swRegistration = swReg;
})
.catch(function (error) {
console.error("Service Worker Error", error);
});
} else {
console.warn("Push messaging is not supported");
pushButton.textContent = "Push Not Supported";
}
}
When checking the console in Chrome, Safari, and Firefox, I noticed the following outputs:
"Service Worker and Push is supported"
"Service Worker is registered >ServiceWorkerRegistration"
"Service Worker Error ReferenceError: swRegistration is not defined"
I'm puzzled as to why swReg can be logged, but swRegistration remains undefined. Any ideas or suggestions?
.then(function (swReg) {
console.log("Service Worker is registered", swReg);
swRegistration = swReg;