-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
35 lines (30 loc) · 1.13 KB
/
Copy pathapp.js
File metadata and controls
35 lines (30 loc) · 1.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
function savePOIOffline(poi) {
const request = indexedDB.open('POIDB', 1);
request.onupgradeneeded = event => {
const db = event.target.result;
if (!db.objectStoreNames.contains('pendingPOIs')) {
db.createObjectStore('pendingPOIs', { keyPath: 'id', autoIncrement: true });
}
};
request.onsuccess = event => {
const db = event.target.result;
const tx = db.transaction('pendingPOIs', 'readwrite');
const store = tx.objectStore('pendingPOIs');
store.add(poi);
// 🧭 Intentamos registrar una sincronización
navigator.serviceWorker.ready.then(swReg => {
return swReg.sync.register('sync-pois');
}).catch(err => {
console.warn('❌ Error al registrar sync:', err);
// 💡 Como fallback, mandamos mensaje directo al SW
navigator.serviceWorker.controller?.postMessage({ type: 'SYNC_POIS' });
});
};
request.onerror = () => {
console.error('❌ Error al guardar el POI offline');
};
}
// ✅ Pide permiso para mostrar notificaciones (una vez)
if ('Notification' in window && Notification.permission !== 'granted') {
Notification.requestPermission();
}