Skip to content

Commit 02095aa

Browse files
committed
feat(rn): 增加notifee用于本地通知
1 parent d3c2c78 commit 02095aa

File tree

5 files changed

+102
-1
lines changed

5 files changed

+102
-1
lines changed

client/mobile/ios/Podfile.lock

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -419,6 +419,11 @@ PODS:
419419
- React
420420
- RNGestureHandler (2.9.0):
421421
- React-Core
422+
- RNNotifee (7.4.0):
423+
- React-Core
424+
- RNNotifee/NotifeeCore (= 7.4.0)
425+
- RNNotifee/NotifeeCore (7.4.0):
426+
- React-Core
422427
- RNReanimated (2.14.4):
423428
- DoubleConversion
424429
- FBLazyVector
@@ -513,6 +518,7 @@ DEPENDENCIES:
513518
- ReactCommon/turbomodule/core (from `../node_modules/react-native/ReactCommon`)
514519
- ReactNativeUiLib (from `../node_modules/react-native-ui-lib`)
515520
- RNGestureHandler (from `../node_modules/react-native-gesture-handler`)
521+
- "RNNotifee (from `../node_modules/@notifee/react-native`)"
516522
- RNReanimated (from `../node_modules/react-native-reanimated`)
517523
- Yoga (from `../node_modules/react-native/ReactCommon/yoga`)
518524

@@ -607,6 +613,8 @@ EXTERNAL SOURCES:
607613
:path: "../node_modules/react-native-ui-lib"
608614
RNGestureHandler:
609615
:path: "../node_modules/react-native-gesture-handler"
616+
RNNotifee:
617+
:path: "../node_modules/@notifee/react-native"
610618
RNReanimated:
611619
:path: "../node_modules/react-native-reanimated"
612620
Yoga:
@@ -662,6 +670,7 @@ SPEC CHECKSUMS:
662670
ReactCommon: f697c0ac52e999aa818e43e2b6f277787c735e2d
663671
ReactNativeUiLib: 8d3804947431a465a69f09c5e785c988314612a9
664672
RNGestureHandler: 071d7a9ad81e8b83fe7663b303d132406a7d8f39
673+
RNNotifee: da8dcf09f079ea22f46e239d7c406e10d4525a5f
665674
RNReanimated: cc5e3aa479cb9170bcccf8204291a6950a3be128
666675
SocketRocket: fccef3f9c5cedea1353a9ef6ada904fde10d6608
667676
Yoga: 5b0304b3dbef2b52e078052138e23a19c7dacaef

client/mobile/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
"test": "jest"
1111
},
1212
"dependencies": {
13+
"@notifee/react-native": "^7.4.0",
1314
"immer": "^9.0.19",
1415
"react": "18.2.0",
1516
"react-native": "0.71.2",

client/mobile/src/AppMain.tsx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
import React from 'react';
1+
import React, { useEffect } from 'react';
22
import { StyleSheet, View } from 'react-native';
33
import { WebView } from 'react-native-webview';
4+
import { initNotificationEnv } from './lib/notifications';
45

56
/**
67
* Tailchat的主要内容
@@ -12,6 +13,10 @@ interface Props {
1213
host: string;
1314
}
1415
export const AppMain: React.FC<Props> = React.memo((props) => {
16+
useEffect(() => {
17+
initNotificationEnv();
18+
}, []);
19+
1520
return (
1621
<View style={styles.root}>
1722
<WebView source={{ uri: props.host }} />
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
import notifee, { EventType } from '@notifee/react-native';
2+
3+
interface NotificationInfo {
4+
title: string;
5+
body: string;
6+
}
7+
8+
// Create a channel (required for Android)
9+
async function createDefaultChannel() {
10+
const channelId = await notifee.createChannel({
11+
id: 'default',
12+
name: 'Default Channel',
13+
});
14+
15+
return channelId;
16+
}
17+
18+
/**
19+
* 显示本地通知
20+
*/
21+
export async function showNotification(info: NotificationInfo) {
22+
// Request permissions (required for iOS)
23+
await notifee.requestPermission();
24+
25+
const channelId = await createDefaultChannel();
26+
27+
// Display a notification
28+
await notifee.displayNotification({
29+
title: info.title,
30+
body: info.body,
31+
android: {
32+
channelId,
33+
// pressAction is needed if you want the notification to open the app when pressed
34+
pressAction: {
35+
id: 'default',
36+
},
37+
},
38+
});
39+
}
40+
41+
export async function initNotificationEnv() {
42+
await notifee.requestPermission();
43+
44+
await initForegroundService();
45+
}
46+
47+
async function initForegroundService() {
48+
notifee.registerForegroundService((_notification) => {
49+
return new Promise(() => {
50+
// 一直pending,因此前台服务会一直存在
51+
52+
notifee.onForegroundEvent(async ({ type, detail }) => {
53+
if (
54+
type === EventType.ACTION_PRESS &&
55+
detail.pressAction?.id === 'stop'
56+
) {
57+
await notifee.stopForegroundService();
58+
}
59+
});
60+
});
61+
});
62+
63+
const channelId = await createDefaultChannel();
64+
65+
notifee.displayNotification({
66+
title: 'Foreground service',
67+
body: 'This notification will exist for the lifetime of the service runner',
68+
android: {
69+
channelId,
70+
asForegroundService: true,
71+
actions: [
72+
{
73+
title: '停止前台服务',
74+
pressAction: {
75+
id: 'stop',
76+
},
77+
},
78+
],
79+
},
80+
});
81+
}

client/mobile/yarn.lock

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1436,6 +1436,11 @@
14361436
"@nodelib/fs.scandir" "2.1.5"
14371437
fastq "^1.6.0"
14381438

1439+
"@notifee/react-native@^7.4.0":
1440+
version "7.4.0"
1441+
resolved "https://registry.npmmirror.com/@notifee/react-native/-/react-native-7.4.0.tgz#0f20744307bf3b800f7b56eb2d0bbdd474748d09"
1442+
integrity sha512-c8pkxDQFRbw0JlUmTb07OTG/4LQHRj8MBodMLwEcO+SvqIxK8ya8zSUEzfdcdWsSVqdoym0v3zpSNroR3Quj/w==
1443+
14391444
"@react-native-community/cli-clean@^10.1.1":
14401445
version "10.1.1"
14411446
resolved "https://registry.npmmirror.com/@react-native-community/cli-clean/-/cli-clean-10.1.1.tgz#4c73ce93a63a24d70c0089d4025daac8184ff504"

0 commit comments

Comments
 (0)