POC: Mobile notification via periodicSync

This commit is contained in:
10G Meow 2023-11-26 13:51:15 +02:00
parent b14749c28f
commit 2aa97cf134
3 changed files with 78 additions and 6 deletions

View file

@ -19,4 +19,18 @@ self.addEventListener("notificationclick", (e) => {
.then((windowClient) => (windowClient ? windowClient.focus() : null));
}),
);
});
});
async function checkNewMessages() {
const cachedCredentials = await caches.open('cachedCredentials');
// Todo...
}
self.addEventListener('periodicsync', (event) => {
if (event.tag === 'check-new-messages') {
// Test if periodicSync notification triggers in Mobile app(created via add to homescreen)
self.registration.showNotification("Notification via periodicSync");
event.waitUntil(checkNewMessages());
}
});

View file

@ -1,6 +1,53 @@
import { isMobileOrTabletBrowser } from './utils'
const registerPeriodicBackgroundSync = async (registration) => {
// Check if periodicSync is supported
if ('periodicSync' in registration) {
// Request permission
const status = await navigator.permissions.query({
name: 'periodic-background-sync',
});
if (status.state === 'granted') {
console.log('PBS registered and granted')
try {
// Register the periodic background sync.
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())
// List registered periodic background sync tags.
const tags = await registration.periodicSync.getTags();
if (tags.length) {
tags.forEach((tag) => {
console.log('tag')
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");
if ('serviceWorker' in navigator) {
window.addEventListener('load', async () => {
const registration = await navigator.serviceWorker.register("/sw.js");
console.log('Service worker registered for scope', registration.scope);
if(isMobileOrTabletBrowser) {
await registerPeriodicBackgroundSync(registration);
}
});
} else {
console.log("No Service Worker support!");
}

View file

@ -486,7 +486,7 @@ class Util {
/**
* Return what "mode" to use for the given room.
*
*
* The default value is given by the room itself (as state events, see roomTypeMixin).
* This method just returns if the user has overridden this in room settings (this
* fact will be persisted as a user specific tag on the room). Note: currently override
@ -515,7 +515,7 @@ class Util {
/**
* Return the room type for the current room
* @param {*} roomOrNull
* @param {*} roomOrNull
*/
roomDisplayTypeToQueryParam(roomOrNull, roomDisplayType) {
const roomType = this.roomDisplayTypeOverride(roomOrNull) || roomDisplayType;
@ -898,7 +898,7 @@ class Util {
}
downloadableTypes() {
return ['m.video','m.audio','m.image','m.file'];
return ['m.video','m.audio','m.image','m.file'];
}
download(matrixClient, event) {
@ -921,6 +921,17 @@ class Util {
console.log("Failed to fetch attachment: ", err);
});
}
isMobileOrTabletBrowser() {
// Regular expression to match common mobile and tablet browser user agent strings
const mobileTabletPattern = /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini|Tablet|Mobile|CriOS/i;
// Get the user agent string
const userAgent = navigator.userAgent;
// Check if the user agent matches the pattern for mobile or tablet browsers
return mobileTabletPattern.test(userAgent);
}
}
export default new Util();