mirror of https://github.com/msgbyte/tailchat
feat(rn): 移动端增加消息通知显示
parent
92d5e39cfc
commit
25084e0422
@ -1,16 +1,73 @@
|
|||||||
import { sharedEvent } from '@capital/common';
|
import {
|
||||||
|
getGlobalState,
|
||||||
|
sharedEvent,
|
||||||
|
getCachedUserInfo,
|
||||||
|
getCachedBaseGroupInfo,
|
||||||
|
} from '@capital/common';
|
||||||
|
import { Translate } from './translate';
|
||||||
|
|
||||||
const PLUGIN_NAME = 'ReactNative支持';
|
const PLUGIN_NAME = 'ReactNative支持';
|
||||||
|
|
||||||
console.log(`Plugin ${PLUGIN_NAME} is loaded`);
|
console.log(`Plugin ${PLUGIN_NAME} is loaded`);
|
||||||
|
|
||||||
sharedEvent.on('loadColorScheme', (colorScheme: string) => {
|
/**
|
||||||
window.postMessage(
|
* 转发事件
|
||||||
{
|
*/
|
||||||
|
function forwardSharedEvent(
|
||||||
|
eventName: string,
|
||||||
|
processPayload?: (payload: any) => Promise<{ type: string; payload: any }>
|
||||||
|
) {
|
||||||
|
if (!(window as any).ReactNativeWebView) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
sharedEvent.on(eventName, async (payload: any) => {
|
||||||
|
let type = eventName;
|
||||||
|
if (processPayload) {
|
||||||
|
const res = await processPayload(payload);
|
||||||
|
payload = res.payload;
|
||||||
|
type = res.type;
|
||||||
|
}
|
||||||
|
|
||||||
|
(window as any).ReactNativeWebView.postMessage(
|
||||||
|
JSON.stringify({
|
||||||
_isTailchat: true,
|
_isTailchat: true,
|
||||||
type: 'loadColorScheme',
|
type,
|
||||||
payload: colorScheme,
|
payload,
|
||||||
},
|
})
|
||||||
'*'
|
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
forwardSharedEvent('loadColorScheme');
|
||||||
|
forwardSharedEvent('receiveUnmutedMessage', async (payload) => {
|
||||||
|
const message = payload;
|
||||||
|
const currentUserId = getGlobalState()?.user.info._id;
|
||||||
|
|
||||||
|
if (currentUserId === message.author) {
|
||||||
|
// 忽略本人消息
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const [userInfo, scopeName] = await Promise.all([
|
||||||
|
getCachedUserInfo(message.author),
|
||||||
|
message.groupId
|
||||||
|
? getCachedBaseGroupInfo(message.groupId).then((d) => d.name)
|
||||||
|
: Promise.resolve(Translate.dm),
|
||||||
|
]);
|
||||||
|
const nickname = userInfo?.nickname ?? '';
|
||||||
|
const icon = userInfo?.avatar ?? undefined;
|
||||||
|
const content = message.content;
|
||||||
|
|
||||||
|
const title = `${Translate.from} [${scopeName}] ${nickname}`;
|
||||||
|
const body = content;
|
||||||
|
|
||||||
|
return {
|
||||||
|
type: 'showNotification',
|
||||||
|
payload: {
|
||||||
|
title,
|
||||||
|
body,
|
||||||
|
icon,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
});
|
||||||
|
@ -0,0 +1,12 @@
|
|||||||
|
import { localTrans } from '@capital/common';
|
||||||
|
|
||||||
|
export const Translate = {
|
||||||
|
from: localTrans({
|
||||||
|
'zh-CN': '来自',
|
||||||
|
'en-US': 'From',
|
||||||
|
}),
|
||||||
|
dm: localTrans({
|
||||||
|
'zh-CN': '私信',
|
||||||
|
'en-US': 'DM',
|
||||||
|
}),
|
||||||
|
};
|
Loading…
Reference in New Issue