From 15a4571a0815a322b0b729a164eb0c8b9083b872 Mon Sep 17 00:00:00 2001 From: moonrailgun Date: Fri, 16 Sep 2022 22:28:11 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=A2=9E=E5=8A=A0sw=E6=8E=A8=E9=80=81?= =?UTF-8?q?=E7=AD=96=E7=95=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../plugins/com.msgbyte.notify/src/notify.ts | 26 ++++++++++++++----- .../plugins/com.msgbyte.notify/tsconfig.json | 1 - client/web/src/plugin/common/index.ts | 1 + client/web/src/utils/sw-helper.ts | 11 ++++++++ 4 files changed, 32 insertions(+), 7 deletions(-) diff --git a/client/web/plugins/com.msgbyte.notify/src/notify.ts b/client/web/plugins/com.msgbyte.notify/src/notify.ts index fa3f02ce..179ada56 100644 --- a/client/web/plugins/com.msgbyte.notify/src/notify.ts +++ b/client/web/plugins/com.msgbyte.notify/src/notify.ts @@ -2,6 +2,7 @@ import { regSocketEventListener, getGlobalState, getCachedUserInfo, + getServiceWorkerRegistration, } from '@capital/common'; export function initNotify() { @@ -24,12 +25,25 @@ export function initNotify() { const icon = userInfo?.avatar ?? undefined; const content = message.content; - new Notification(`来自 ${nickname}`, { - body: content, - icon, - tag: 'tailchat-message', - renotify: true, - }); + const registration: ServiceWorkerRegistration | null = + getServiceWorkerRegistration(); + + if (registration) { + registration.showNotification(`来自 ${nickname}`, { + body: content, + icon, + tag: 'tailchat-message', + renotify: true, + }); + } else { + // fallback + new Notification(`来自 ${nickname}`, { + body: content, + icon, + tag: 'tailchat-message', + renotify: true, + }); + } } ); } diff --git a/client/web/plugins/com.msgbyte.notify/tsconfig.json b/client/web/plugins/com.msgbyte.notify/tsconfig.json index beb3c60f..465a28b5 100644 --- a/client/web/plugins/com.msgbyte.notify/tsconfig.json +++ b/client/web/plugins/com.msgbyte.notify/tsconfig.json @@ -1,6 +1,5 @@ { "compilerOptions": { - "rootDir": "./src", "baseUrl": "./src", "esModuleInterop": true, "jsx": "react", diff --git a/client/web/src/plugin/common/index.ts b/client/web/src/plugin/common/index.ts index e78294b7..28289eb7 100644 --- a/client/web/src/plugin/common/index.ts +++ b/client/web/src/plugin/common/index.ts @@ -27,6 +27,7 @@ export { urlSearchParse, appendUrlSearch, } from '@/utils/url-helper'; +export { getServiceWorkerRegistration } from '@/utils/sw-helper'; export { useGroupIdContext } from '@/context/GroupIdContext'; import { request, RequestConfig, useUserInfo } from 'tailchat-shared'; export { diff --git a/client/web/src/utils/sw-helper.ts b/client/web/src/utils/sw-helper.ts index e3ac72a9..66b9371a 100644 --- a/client/web/src/utils/sw-helper.ts +++ b/client/web/src/utils/sw-helper.ts @@ -19,6 +19,8 @@ const handleShowUpdateTip = _once(() => { }, 2000); }); +let _serviceWorkerRegistration: ServiceWorkerRegistration | null = null; + /** * 处理registration相关任务和状态 */ @@ -67,6 +69,8 @@ export function installServiceWorker() { .then((registration) => { console.log('SW registered: ', registration); + _serviceWorkerRegistration = registration; + handleRegistration(registration); }) .catch((registrationError) => { @@ -75,3 +79,10 @@ export function installServiceWorker() { }); } } + +/** + * 获取SW的Registration + */ +export function getServiceWorkerRegistration(): ServiceWorkerRegistration | null { + return _serviceWorkerRegistration; +}