refactor(mobile): move socket into foreground service

pull/90/head
moonrailgun 2 years ago
parent 91a6721671
commit bd56d517e8

@ -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',

@ -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<Props> = React.memo((props) => {
const webviewRef = useRef<WebView>(null);
useEffect(() => {
initNotificationEnv();
}, []);
return (
<View style={styles.root}>
<WebView

@ -1,8 +1,8 @@
import type WebView from 'react-native-webview';
import { generateInstallPluginScript } from '.';
import { useUIStore } from '../../store/ui';
import { showNotification } from '../notifications';
import type { UserBaseInfo } from '../../types';
import { UserBaseInfo } from '../../types';
import { initNotificationEnv, showNotification } from '../notifications';
import { bindSocketEvent, createSocket } from '../socket';
export function handleTailchatMessage(
@ -38,9 +38,14 @@ export function handleTailchatMessage(
const token: string = payload.token;
const userInfo = payload.userInfo as UserBaseInfo;
createSocket(serviceUrl, token).then((socket) => {
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);
});
},
});
}
}

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

Loading…
Cancel
Save