From c567db944ae1bef703f4a9a07d032885d2ac1a9b Mon Sep 17 00:00:00 2001 From: Vasilii Mikhailovskii Date: Tue, 3 Dec 2024 00:57:27 +0200 Subject: [PATCH] iOS sw --- apps/frontend/public/service-worker.js | 38 ++++++++++++++++++++++---- 1 file changed, 33 insertions(+), 5 deletions(-) diff --git a/apps/frontend/public/service-worker.js b/apps/frontend/public/service-worker.js index d8c61a0..07a8b62 100644 --- a/apps/frontend/public/service-worker.js +++ b/apps/frontend/public/service-worker.js @@ -8,6 +8,8 @@ self.addEventListener('install', (event) => { // of event.waitUntil(); }) +const isIOS = /iPhone|iPad|iPod/i.test(navigator.userAgent) + self.addEventListener('activate', (event) => { event.waitUntil( clients.claim(), // Привязка нового сервис-воркера к текущим клиентам @@ -114,26 +116,52 @@ self.addEventListener('notificationclick', (event) => { type: 'notificationClick', tag: event.notification.tag, payload: event.notification.payload, + url: urlToOpen, matchUrl: true, + isIOS, }), ]) } } + if (isIOS && clientList.length > 0) { + const client = clientList[0] + return Promise.all([ + client.focus(), + client.postMessage({ + type: 'notificationClick', + tag: event.notification.tag, + payload: event.notification.payload, + url: urlToOpen, + matchUrl: false, + isIOS, + }), + ]) + } + if (clients.openWindow) { console.log('open new window', urlToOpen) return clients.openWindow(urlToOpen).then((client) => { - if (!client) { + if (client) { + return client.postMessage({ + type: 'notificationClick', + tag: event.notification.tag, + payload: event.notification.payload, + url: urlToOpen, + matchUrl: true, + isIOS, + }) + } else { console.log('new window not opened') for (const client of clientList) { return client.postMessage({ type: 'notificationClick', - url: urlToOpen, tag: event.notification.tag, payload: event.notification.payload, + url: urlToOpen, matchUrl: false, - msg: 'new window not opened', + isIOS, }) } } @@ -144,11 +172,11 @@ self.addEventListener('notificationclick', (event) => { for (const client of clientList) { return client.postMessage({ type: 'notificationClick', - url: urlToOpen, tag: event.notification.tag, payload: event.notification.payload, + url: urlToOpen, matchUrl: false, - msg: 'clients.openWindow not available', + isIOS, }) } }