I'm trying to subscribe the user but this is the error I get the first time. The second time, it is working because the user is already active
This is my code :
if ('serviceWorker' in navigator) {
console.log('Service Worker is supported');
navigator.serviceWorker.register('sw.js').then(function(reg) {
console.log(':^)', reg);
reg.pushManager.subscribe({
userVisibleOnly: true
}).then(function(sub) {
console.log('endpoint:', sub.endpoint);
var div = document.getElementById('id');
div.innerHTML = div.innerHTML + sub.endpoint;
// var b = document.getElementsByTagName('body')[0];
//b.appendChild(div);
//alert(sub.endpoint);
});
});
}
I already tried the following, but the ready.then()
method is not happening:
if ('serviceWorker' in navigator) {
console.log('Service Worker is supported');
navigator.serviceWorker.register('sw.js').then(function(sreg) {
console.log(':^)', sreg);
navigator.serviceWorker.ready.then(function(reg) {
reg.pushManager.subscribe({
userVisibleOnly: true
}).then(function(sub) {
console.log('endpoint:', sub.endpoint);
var div = document.getElementById('id');
div.innerHTML = div.innerHTML + sub.endpoint;
// var b = document.getElementsByTagName('body')[0];
//b.appendChild(div);
//alert(sub.endpoint);
});
});
});
}
Any ideas ?
You should wait for the service worker to be activated before triggering the subscription.
use stateChange listener for figuring out if service worker is active
navigator.serviceWorker.register(workerFileName, {scope: "/"})
.then(
function (reg) {
var serviceWorker;
if (reg.installing) {
serviceWorker = reg.installing;
// console.log('Service worker installing');
} else if (reg.waiting) {
serviceWorker = reg.waiting;
// console.log('Service worker installed & waiting');
} else if (reg.active) {
serviceWorker = reg.active;
// console.log('Service worker active');
}
if (serviceWorker) {
console.log("sw current state", serviceWorker.state);
if (serviceWorker.state == "activated") {
//If push subscription wasnt done yet have to do here
console.log("sw already activated - Do watever needed here");
}
serviceWorker.addEventListener("statechange", function(e) {
console.log("sw statechange : ", e.target.state);
if (e.target.state == "activated") {
// use pushManger for subscribing here.
console.log("Just now activated. now we can subscribe for push notification")
subscribeForPushNotification(reg);
}
});
}
},
function (err) {
console.error('unsuccessful registration with ', workerFileName, err);
}
);
You can resolve this by checking for service worker status. Perform your actions only when the service worker is active.
navigator.serviceWorker.register('sw.js').then(function(reg) {
if(reg.installing) {
console.log('Service worker installing');
} else if(reg.waiting) {
console.log('Service worker installed');
} else if(reg.active) {
console.log('Service worker active');
}
// Include below mentioned validations
}
Check for pushnotification is supported or not using
// Check if push messaging is supported
if (!('PushManager' in window)) {
console.log('Push messaging isn\'t supported.');
return;
}
//
if (Notification.permission === 'denied') {
console.log('The user has blocked notifications.');
return;
}
After these two checks implement you functionality.
navigator.serviceWorker.ready.then(function(reg) { .... })
Hope this helps
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With