feat: 增加sw推送策略

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

@ -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,
});
}
}
);
}

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

@ -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 {

@ -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() {
});
}
}
/**
* SWRegistration
*/
export function getServiceWorkerRegistration(): ServiceWorkerRegistration | null {
return _serviceWorkerRegistration;
}

Loading…
Cancel
Save