fix: 修复不支持Notification的浏览器会直接抛出异常的bug

pull/13/head
moonrailgun 3 years ago
parent cd4844dbef
commit 7bf81152f5

@ -1,36 +1,7 @@
import {
regSocketEventListener,
getGlobalState,
getCachedUserInfo,
} from '@capital/common';
import { initNotify } from 'notify';
if (Notification.permission === 'default') {
Notification.requestPermission();
if ('Notification' in window) {
initNotify();
} else {
console.warn('浏览器不支持 Notification');
}
regSocketEventListener({
eventName: 'chat.message.add',
eventFn: (message) => {
const currentUserId = getGlobalState()?.user.info._id;
if (currentUserId !== message.author) {
// 创建通知
// TODO: 需要增加所在群组
if (Notification.permission === 'granted') {
Promise.all([getCachedUserInfo(currentUserId)]).then(([userInfo]) => {
console.log(userInfo, message);
const nickname = userInfo?.nickname ?? '';
const icon = userInfo?.avatar ?? undefined;
const content = message.content;
new Notification(`来自 ${nickname}`, {
body: content,
icon,
tag: 'tailchat-message',
renotify: true,
});
});
}
}
},
});

@ -0,0 +1,38 @@
import {
regSocketEventListener,
getGlobalState,
getCachedUserInfo,
} from '@capital/common';
export function initNotify() {
if (Notification.permission === 'default') {
Notification.requestPermission();
}
regSocketEventListener({
eventName: 'chat.message.add',
eventFn: (message) => {
const currentUserId = getGlobalState()?.user.info._id;
if (currentUserId !== message.author) {
// 创建通知
// TODO: 需要增加所在群组
if (Notification.permission === 'granted') {
Promise.all([getCachedUserInfo(currentUserId)]).then(([userInfo]) => {
console.log(userInfo, message);
const nickname = userInfo?.nickname ?? '';
const icon = userInfo?.avatar ?? undefined;
const content = message.content;
new Notification(`来自 ${nickname}`, {
body: content,
icon,
tag: 'tailchat-message',
renotify: true,
});
});
}
}
},
});
}
Loading…
Cancel
Save