I am using React, Redux and service worker at my application and I want to access the Redux store at my service worker.
My service worker is:
function checkCompatibility() {
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);
})
.catch(function (error) {
console.error('Service Worker Error', error);
});
} else {
console.warn('Push messaging is not supported');
}
}
function askForNotification() {
if (!('Notification' in window)) {
console.log('This browser does not support desktop notification');
} else if (Notification.permission === 'granted') {
console.log('Permission granted.');
} else if (Notification.permission !== 'denied' || Notification.permission === 'default') {
Notification.requestPermission(function (permission) {
if (permission === 'granted') {
var notification = new Notification('Permissão de Notificação foi Aceita', { icon: '/favicon.png', badge: '/favicon.png' });
}
});
}
}
checkCompatibility();
askForNotification();
Can anyone help me?
Service Workers are built on top of the Web Worker API. According to the MDN documentation on Web Workers:
workers run in another global context that is different from the current window
Thus the service worker is in a different scope from your Redux code.
However, you might be able to use the postMessage API to communicate between your main application code and the service worker, and pass whatever data you need from Redux via this channel.
Take a look at the code sample here for inspiration.
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