Merge branch '419-notifications-via-periodicsync-poc' into 'dev'
POC: Periodic Background Synchronization See merge request keanuapp/keanuapp-weblite!269
This commit is contained in:
commit
9ec91b3f2a
3 changed files with 58 additions and 9 deletions
17
public/sw.js
17
public/sw.js
|
|
@ -1,8 +1,6 @@
|
|||
// Notification click event listener
|
||||
self.addEventListener("notificationclick", (e) => {
|
||||
// Close the notification popout
|
||||
e.notification.close();
|
||||
// Get all the Window clients
|
||||
e.waitUntil(
|
||||
clients.matchAll({ type: "window" }).then((clientsArr) => {
|
||||
// If a Window tab matching the targeted URL already exists, focus that;
|
||||
|
|
@ -20,3 +18,18 @@ self.addEventListener("notificationclick", (e) => {
|
|||
}),
|
||||
);
|
||||
});
|
||||
|
||||
async function checkNewMessages() {
|
||||
const cachedCredentials = await caches.open('cachedCredentials');
|
||||
// Todo...
|
||||
}
|
||||
|
||||
// Install PWA in mobile or web to test if periodicSync notification works
|
||||
// see browser compatibility: https://developer.mozilla.org/en-US/docs/Web/API/Web_Periodic_Background_Synchronization_API#browser_compatibility
|
||||
self.addEventListener('periodicsync', (event) => {
|
||||
if (event.tag === 'check-new-messages') {
|
||||
self.registration.showNotification("Notification via periodicSync");
|
||||
|
||||
event.waitUntil(checkNewMessages());
|
||||
}
|
||||
});
|
||||
|
|
|
|||
|
|
@ -1,8 +1,44 @@
|
|||
// Installing your PWA is required for periodic syncs to work
|
||||
const registerPeriodicBackgroundSync = async (registration) => {
|
||||
if ('periodicSync' in registration) {
|
||||
const status = await navigator.permissions.query({
|
||||
name: 'periodic-background-sync',
|
||||
});
|
||||
if (status.state === 'granted') {
|
||||
console.log('Periodic background sync registered and granted')
|
||||
|
||||
try {
|
||||
await registration.periodicSync.register('check-new-messages', {
|
||||
// minInterval is one day
|
||||
minInterval: 24 * 60 * 60 * 1000,
|
||||
});
|
||||
console.log('Periodic background sync registered!');
|
||||
console.log(registration.periodicSync.getTags())
|
||||
|
||||
const tags = await registration.periodicSync.getTags();
|
||||
if (tags.length) {
|
||||
tags.forEach((tag) => {
|
||||
console.log('tag name')
|
||||
console.log(tag)
|
||||
});
|
||||
}
|
||||
} catch(e) {
|
||||
console.log(`Periodic background sync failed: ${e}`);
|
||||
}
|
||||
} else {
|
||||
console.log('Periodic background sync is not granted.');
|
||||
}
|
||||
} else {
|
||||
console.log('Periodic background sync is not supported.');
|
||||
}
|
||||
}
|
||||
|
||||
export function registerServiceWorker() {
|
||||
if("serviceWorker" in navigator) {
|
||||
navigator.serviceWorker.register("./sw.js")
|
||||
.then(registration => {
|
||||
.then(async registration => {
|
||||
console.log('Service Worker registered with scope:', registration.scope);
|
||||
await registerPeriodicBackgroundSync(registration);
|
||||
})
|
||||
.catch(error => {
|
||||
console.error('Service Worker registration failed:', error);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue