|
|
@ -3,6 +3,7 @@ import {
|
|
|
|
getGlobalState,
|
|
|
|
getGlobalState,
|
|
|
|
getCachedUserInfo,
|
|
|
|
getCachedUserInfo,
|
|
|
|
getServiceWorkerRegistration,
|
|
|
|
getServiceWorkerRegistration,
|
|
|
|
|
|
|
|
navigate,
|
|
|
|
} from '@capital/common';
|
|
|
|
} from '@capital/common';
|
|
|
|
import { Translate } from './translate';
|
|
|
|
import { Translate } from './translate';
|
|
|
|
import { hasSilent } from './silent';
|
|
|
|
import { hasSilent } from './silent';
|
|
|
@ -12,6 +13,18 @@ export function initNotify() {
|
|
|
|
Notification.requestPermission();
|
|
|
|
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({
|
|
|
|
regSocketEventListener({
|
|
|
|
eventName: 'chat.message.add',
|
|
|
|
eventName: 'chat.message.add',
|
|
|
|
eventFn: (message) => {
|
|
|
|
eventFn: (message) => {
|
|
|
@ -34,21 +47,26 @@ 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();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const title = `${Translate.from} ${nickname}`;
|
|
|
|
const title = `${Translate.from} ${nickname}`;
|
|
|
|
const options: NotificationOptions = {
|
|
|
|
const options: NotificationOptions = {
|
|
|
|
body: content,
|
|
|
|
body: content,
|
|
|
|
icon,
|
|
|
|
icon,
|
|
|
|
tag: 'tailchat-message',
|
|
|
|
tag: 'tailchat-message',
|
|
|
|
renotify: true,
|
|
|
|
renotify: true,
|
|
|
|
|
|
|
|
data: message,
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
if (registration && registration.showNotification) {
|
|
|
|
if (registration && registration.showNotification) {
|
|
|
|
registration.showNotification(title, options);
|
|
|
|
registration.showNotification(title, options);
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
// fallback
|
|
|
|
// 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);
|
|
|
|
|
|
|
|
}
|
|
|
|