Comments (1)
POC from https://web-push-book.gauntface.com/chapter-05/02-display-a-notification/
service-worker.js
const examplePage = '/demos/notification-examples/example-page.html';
function focusWindow(event) {
const urlToOpen = new URL(examplePage, self.location.origin).href;
const promiseChain = clients.matchAll({
type: 'window',
includeUncontrolled: true
})
.then((windowClients) => {
let matchingClient = null;
for (let i = 0; i < windowClients.length; i++) {
const windowClient = windowClients[i];
if (windowClient.url === urlToOpen) {
matchingClient = windowClient;
break;
}
}
if (matchingClient) {
return matchingClient.focus();
} else {
return clients.openWindow(urlToOpen);
}
});
event.waitUntil(promiseChain);
}
self.addEventListener('notificationclick', function(event) {
event.notification.close();
focusWindow(event);
});
self.addEventListener('notificationclose', function(event) {
const dismissedNotification = event.notification;
event.waitUntil(Promise.resolve());
});
index.html
<!DOCTYPE html>
<html>
<body>
<button onclick="mybutton()">Focus a Window OR Open</button>
<script type="text/javascript">
function registerServiceWorker() {
return navigator.serviceWorker.register('service-worker.js')
.then(function(registration) {
console.log('Service worker successfully registered.');
return registration;
})
.catch(function(err) {
console.error('Unable to register service worker.', err);
});
}
window.addEventListener('load', function() {
return registerServiceWorker()
.then(function(registration) {
window.registration = registration
navigator.serviceWorker.addEventListener('message', function(event) {
console.log('Received a message from service worker: ', event.data);
});
})
})
function mybutton(){
const options = {
body: 'Clicking on this notification will focus on an open window ' +
'otherwise open a new one.',
tag: 'focus-window'
};
registration.showNotification('Focus or Open a Window', options);
};
</script>
</body>
</html>
from void-mail.
Related Issues (11)
- UnhandledPromiseRejectionWarning (again -> https://github.com/synox/void-mail/issues/9) HOT 2
- Suggestion - Add email GET request counter HOT 2
- error Failed at the [email protected] start script.
- On npm install it can't find the synox/imap-simple package HOT 1
- Docker HOT 1
- Heroku Application Error
- Feature: Delete Emails
- Feature: Multiple Domains HOT 1
- Privacy Policy
- UnhandledPromiseRejectionWarning: Handle missing To field HOT 9
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from void-mail.