From 1290c1e42646ff4fbf1e36ee5a7cd19750485774 Mon Sep 17 00:00:00 2001 From: moonrailgun Date: Mon, 9 Jan 2023 00:10:29 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20notify=E5=A2=9E=E5=8A=A0=E7=82=B9?= =?UTF-8?q?=E5=87=BB=E8=BE=93=E5=85=A5=E6=A1=86=E8=B7=B3=E8=BD=AC=E7=9A=84?= =?UTF-8?q?=E5=8A=9F=E8=83=BD=EF=BC=8C=E5=B9=B6=E5=A2=9E=E5=8A=A0api?= =?UTF-8?q?=E8=B0=83=E7=94=A8=E9=A1=B5=E9=9D=A2=E8=B7=B3=E8=BD=AC=E7=9A=84?= =?UTF-8?q?=E6=96=B9=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../plugins/com.msgbyte.notify/src/notify.ts | 51 +++++++++++++++++-- client/web/src/App.tsx | 2 + client/web/src/components/AppRouterApi.tsx | 15 ++++++ client/web/src/plugin/common/index.ts | 1 + 4 files changed, 65 insertions(+), 4 deletions(-) create mode 100644 client/web/src/components/AppRouterApi.tsx diff --git a/client/web/plugins/com.msgbyte.notify/src/notify.ts b/client/web/plugins/com.msgbyte.notify/src/notify.ts index e5d209f3..84e2bcf2 100644 --- a/client/web/plugins/com.msgbyte.notify/src/notify.ts +++ b/client/web/plugins/com.msgbyte.notify/src/notify.ts @@ -3,6 +3,7 @@ import { getGlobalState, getCachedUserInfo, getServiceWorkerRegistration, + navigate, } from '@capital/common'; import { Translate } from './translate'; import { hasSilent } from './silent'; @@ -12,6 +13,18 @@ export function initNotify() { Notification.requestPermission(); } + const registration: ServiceWorkerRegistration | null = + getServiceWorkerRegistration(); + + if (registration) { + registration.addEventListener('notificationclick', (e: any) => { + const tag = e.notification.tag; + const data = e.notification.data; + + handleMessageNotifyClick(tag, data); + }); + } + regSocketEventListener({ eventName: 'chat.message.add', eventFn: (message) => { @@ -34,21 +47,26 @@ export function initNotify() { const icon = userInfo?.avatar ?? undefined; const content = message.content; - const registration: ServiceWorkerRegistration | null = - getServiceWorkerRegistration(); - const title = `${Translate.from} ${nickname}`; const options: NotificationOptions = { body: content, icon, tag: 'tailchat-message', renotify: true, + data: message, }; + if (registration && registration.showNotification) { registration.showNotification(title, options); } else { // fallback - new Notification(title, options); + const notification = new Notification(title, options); + notification.onclick = (e: any) => { + const tag = e.target.tag; + const data = e.target.data; + + handleMessageNotifyClick(tag, data); + }; } } ); @@ -57,3 +75,28 @@ export function initNotify() { }, }); } + +/** + * 点击通知栏事件 + */ +function handleMessageNotifyClick(tag, data) { + if (tag === 'tailchat-message') { + const message = data; + + window.focus(); + const { converseId, groupId } = message ?? {}; + if (!converseId) { + console.warn('[notify] Not found converseId'); + return; + } + if (groupId) { + // 群组消息 + navigate(`/main/group/${groupId}/${converseId}`); + } else { + // 私信会话 + navigate(`/main/personal/converse/${converseId}`); + } + } + + console.log(tag, data); +} diff --git a/client/web/src/App.tsx b/client/web/src/App.tsx index 9033d83c..d552cf00 100644 --- a/client/web/src/App.tsx +++ b/client/web/src/App.tsx @@ -22,6 +22,7 @@ import { LoadingSpinner } from './components/LoadingSpinner'; import { pluginRootRoute } from './plugin/common'; import { PortalHost as FallbackPortalHost } from './components/Portal'; import isElectron from 'is-electron'; +import { AppRouterApi } from './components/AppRouterApi'; const AppRouter: any = isElectron() ? HashRouter : BrowserRouter; @@ -108,6 +109,7 @@ export const App: React.FC = React.memo(() => { + { + throw new Error('route navigate not init'); +}; + +export const AppRouterApi: React.FC = React.memo(() => { + const _navigate = useNavigate(); + navigate = _navigate; + + return null; +}); +AppRouterApi.displayName = 'AppRouterApi'; diff --git a/client/web/src/plugin/common/index.ts b/client/web/src/plugin/common/index.ts index aa5dbd2a..ef0a584e 100644 --- a/client/web/src/plugin/common/index.ts +++ b/client/web/src/plugin/common/index.ts @@ -56,6 +56,7 @@ export { joinArray, } from 'tailchat-shared'; +export { navigate } from '@/components/AppRouterApi'; export { useLocation, useNavigate } from 'react-router'; export {