feat: 增加sw推送策略

pull/49/head
moonrailgun 3 years ago
parent c1eaf99911
commit 15a4571a08

@ -2,6 +2,7 @@ import {
regSocketEventListener, regSocketEventListener,
getGlobalState, getGlobalState,
getCachedUserInfo, getCachedUserInfo,
getServiceWorkerRegistration,
} from '@capital/common'; } from '@capital/common';
export function initNotify() { export function initNotify() {
@ -24,6 +25,18 @@ export function initNotify() {
const icon = userInfo?.avatar ?? undefined; const icon = userInfo?.avatar ?? undefined;
const content = message.content; const content = message.content;
const registration: ServiceWorkerRegistration | null =
getServiceWorkerRegistration();
if (registration) {
registration.showNotification(`来自 ${nickname}`, {
body: content,
icon,
tag: 'tailchat-message',
renotify: true,
});
} else {
// fallback
new Notification(`来自 ${nickname}`, { new Notification(`来自 ${nickname}`, {
body: content, body: content,
icon, icon,
@ -31,6 +44,7 @@ export function initNotify() {
renotify: true, renotify: true,
}); });
} }
}
); );
} }
} }

@ -1,6 +1,5 @@
{ {
"compilerOptions": { "compilerOptions": {
"rootDir": "./src",
"baseUrl": "./src", "baseUrl": "./src",
"esModuleInterop": true, "esModuleInterop": true,
"jsx": "react", "jsx": "react",

@ -27,6 +27,7 @@ export {
urlSearchParse, urlSearchParse,
appendUrlSearch, appendUrlSearch,
} from '@/utils/url-helper'; } from '@/utils/url-helper';
export { getServiceWorkerRegistration } from '@/utils/sw-helper';
export { useGroupIdContext } from '@/context/GroupIdContext'; export { useGroupIdContext } from '@/context/GroupIdContext';
import { request, RequestConfig, useUserInfo } from 'tailchat-shared'; import { request, RequestConfig, useUserInfo } from 'tailchat-shared';
export { export {

@ -19,6 +19,8 @@ const handleShowUpdateTip = _once(() => {
}, 2000); }, 2000);
}); });
let _serviceWorkerRegistration: ServiceWorkerRegistration | null = null;
/** /**
* registration * registration
*/ */
@ -67,6 +69,8 @@ export function installServiceWorker() {
.then((registration) => { .then((registration) => {
console.log('SW registered: ', registration); console.log('SW registered: ', registration);
_serviceWorkerRegistration = registration;
handleRegistration(registration); handleRegistration(registration);
}) })
.catch((registrationError) => { .catch((registrationError) => {
@ -75,3 +79,10 @@ export function installServiceWorker() {
}); });
} }
} }
/**
* SWRegistration
*/
export function getServiceWorkerRegistration(): ServiceWorkerRegistration | null {
return _serviceWorkerRegistration;
}

Loading…
Cancel
Save