Die DVZ sucht studentische Hilfskräfte! Weitere Informationen…

Commit 315bd49a authored by Lucas Engelke's avatar Lucas Engelke
Browse files

add caching and notification request from lecture

parent 67a270f0
# Kundenverwaltung
## Development
- `cd public`
- `php -S localhost:8000`
......@@ -16,4 +16,6 @@
<a class="nav-link" href="imprint.php">Impressum</a>
</li>
</ul>
</nav>
\ No newline at end of file
</nav>
<div id="offlineHint" class="alert alert-danger d-none">Die Anwendung ist offline!</div>
<script src="js/header.js"></script>
\ No newline at end of file
......@@ -136,6 +136,13 @@
</aside>
</main>
<?php include('../components/footer.php'); ?>
<script>
if ('serviceWorker' in navigator) {
navigator.serviceWorker.register('sw.js')
.then(reg => console.log(reg))
.catch(err => console.log(err));
}
</script>
</body>
</html>
\ No newline at end of file
window.addEventListener("load", init);
function init() {
updateOnlineStatus();
window.addEventListener('online', updateOnlineStatus);
window.addEventListener('offline', updateOnlineStatus);
Notification.requestPermission();
}
function updateOnlineStatus(event) {
let offlineHint = document.getElementById("offlineHint");
if (navigator.onLine) {
offlineHint.classList.add("d-none");
} else {
offlineHint.classList.remove("d-none");
}
}
\ No newline at end of file
const CACHE_KEY = 'wca_kundenverwaltung';
self.addEventListener('install', event => event.waitUntil(
caches.open(CACHE_KEY)
.then(cache => {
cache.add('/');
cache.add('/manifest.webmanifest');
cache.add('/index.php');
cache.add('/details.php');
cache.add('/edit.php');
cache.add('/imprint.php');
cache.add('/liste.php');
cache.add('/settings.php');
cache.add('/favicon.ico');
cache.add('/sw.js');
cache.add('/img/profil.png');
cache.add('/img/team.png');
cache.add('/css/bootstrap.min.css');
cache.add('/css/bootstrap.min.css.map');
cache.add('/css/main.css');
cache.add('/js/edit.js');
cache.add('/js/rangeOutput.js');
cache.add('/js/header.js');
})
));
self.addEventListener('fetch', function(event) {
event.respondWith(
fetch(event.request).catch(function() {
return caches.match(event.request);
})
);
});
// Handle Events on interaction with notifications
self.addEventListener('notificationclick', refreshBadge);
self.addEventListener('notificationclose', refreshBadge);
function refreshBadge(event) {
if ('setAppBadge' in self.navigator) {
// Get current notifications
self.registration.getNotifications().then(function (notifications) {
// get count of current notifications
numOfNotifications = notifications.length;
if (numOfNotifications > 0) {
// set number of notifications as badge
self.navigator.setAppBadge(numOfNotifications);
} else {
// clear badge
self.navigator.clearAppBadge();
}
});
}
}
// handle push events
self.addEventListener('push', event => {
let numOfNotifications;
if (Notification.permission === 'granted') {
// show notification
event.waitUntil(
self.registration.showNotification('Neue Nachricht!', { body: event.data.text(), icon: 'img/icon512.png' })
);
// refresh badge
if ('setAppBadge' in self.navigator) {
self.registration.getNotifications().then(function (notifications) {
numOfNotifications = notifications.length;
if (numOfNotifications > 0)
self.navigator.setAppBadge(numOfNotifications);
else self.navigator.setAppBadge();
});
}
} else if ('setAppBadge' in self.navigator) {
self.navigator.setAppBadge();
}
});
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment