From bd56d517e84f8c1fe7dfc21029c73b5e32a855a5 Mon Sep 17 00:00:00 2001 From: moonrailgun Date: Tue, 21 Mar 2023 16:27:56 +0800 Subject: [PATCH] refactor(mobile): move socket into foreground service --- client/mobile/scripts/notify-links.js | 8 ++++---- client/mobile/src/AppMain.tsx | 7 +------ client/mobile/src/lib/inject/message-handler.ts | 15 ++++++++++----- client/mobile/src/lib/notifications/index.ts | 13 +++++++++---- 4 files changed, 24 insertions(+), 19 deletions(-) diff --git a/client/mobile/scripts/notify-links.js b/client/mobile/scripts/notify-links.js index aff17ee3..04e167f2 100644 --- a/client/mobile/scripts/notify-links.js +++ b/client/mobile/scripts/notify-links.js @@ -14,14 +14,14 @@ if (tailchatSubscribeId) { if (fs.existsSync(path.join(os.homedir(), './SUCCESS'))) { const links = JSON.parse(artifactLinks) ?? []; const text = - '[b]App 构建成功:[/b]\n' + + '[b]App Building Success:[/b]\n' + links .map((link) => { const { name, url, md5, versionName } = link; return ( `${name}\n` + `version: ${versionName}(${md5})\n` + - `[url=${url}]下载安装包[/url]` + `[url=${url}]Download[/url]` ); }) .join('\n=========\n'); @@ -33,8 +33,8 @@ if (tailchatSubscribeId) { ); } else { const text = - '[b]App 构建失败:[/b]\n' + - `[url=https://codemagic.io/app/${process.env['CM_PROJECT_ID']}/build/${process.env['CM_BUILD_ID']}]查看详情[/url]`; + '[b]App Building Failed:[/b]\n' + + `[url=https://codemagic.io/app/${process.env['CM_PROJECT_ID']}/build/${process.env['CM_BUILD_ID']}]View Detail[/url]`; tailchat.sendSimpleNotify( 'https://tailchat-nightly.moonrailgun.com', diff --git a/client/mobile/src/AppMain.tsx b/client/mobile/src/AppMain.tsx index c8863d4a..e92a55fd 100644 --- a/client/mobile/src/AppMain.tsx +++ b/client/mobile/src/AppMain.tsx @@ -1,9 +1,8 @@ -import React, { useEffect, useRef } from 'react'; +import React, { useRef } from 'react'; import { StyleSheet, View } from 'react-native'; import { WebView } from 'react-native-webview'; import { generatePostMessageScript } from './lib/inject'; import { handleTailchatMessage } from './lib/inject/message-handler'; -import { initNotificationEnv } from './lib/notifications'; /** * Tailchat的主要内容 @@ -17,10 +16,6 @@ interface Props { export const AppMain: React.FC = React.memo((props) => { const webviewRef = useRef(null); - useEffect(() => { - initNotificationEnv(); - }, []); - return ( { - console.log('[createSocket]', 'socket', socket, 'userInfo', userInfo); - bindSocketEvent(socket); + initNotificationEnv({ + nickname: userInfo.nickname ?? userInfo.email, + runService: () => { + createSocket(serviceUrl, token).then((socket) => { + console.log('[createSocket]', 'socket', socket, 'userInfo', userInfo); + bindSocketEvent(socket); + }); + }, }); } } diff --git a/client/mobile/src/lib/notifications/index.ts b/client/mobile/src/lib/notifications/index.ts index 43dcd3f1..bc3362a7 100644 --- a/client/mobile/src/lib/notifications/index.ts +++ b/client/mobile/src/lib/notifications/index.ts @@ -42,16 +42,21 @@ export async function showNotification(info: NotificationInfo) { }); } -export async function initNotificationEnv() { +interface NotificationOptions { + nickname: string; + runService: () => void; +} +export async function initNotificationEnv(options: NotificationOptions) { await notifee.requestPermission(); - await initForegroundService(); + await initForegroundService(options); } -async function initForegroundService() { +async function initForegroundService(options: NotificationOptions) { notifee.registerForegroundService((_notification) => { return new Promise(() => { // 一直pending,因此前台服务会一直存在 + options.runService(); notifee.onForegroundEvent(async ({ type, detail }) => { if ( @@ -67,7 +72,7 @@ async function initForegroundService() { const channelId = await createDefaultChannel(); notifee.displayNotification({ - title: 'Tailchat', + title: `Tailchat: ${options.nickname}`, body: '持续保持服务正常运行, 关闭后可能无法正常接受到消息推送', android: { channelId,