forked from react-native-webrtc/react-native-webrtc
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathuseForegroundService.ts
More file actions
96 lines (88 loc) · 2.71 KB
/
Copy pathuseForegroundService.ts
File metadata and controls
96 lines (88 loc) · 2.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import { useEffect, useState } from 'react';
import { NativeModules, PermissionsAndroid, Platform } from 'react-native';
const { WebRTCModule } = NativeModules;
export type ForegroundServiceConfig = {
enableCamera?: boolean;
enableMicrophone?: boolean;
enableScreenSharing?: boolean;
channelId?: string;
channelName?: string;
notificationTitle?: string;
notificationContent?: string;
/**
* Notification channel importance. Defaults to `'high'` (heads-up popup with sound).
* Use `'low'` for a silent, persistent ongoing-call indicator.
*
* Note: Android binds the importance to the channel on first creation and ignores
* later changes for the same `channelId`. If you need to switch importance at
* runtime, use a distinct `channelId` per level (e.g. `"...channel.low"` vs
* `"...channel.high"`).
*/
importance?: 'low' | 'high';
};
const requestNotificationsPermission = async () => {
try {
const result = await PermissionsAndroid.request(
PermissionsAndroid.PERMISSIONS.POST_NOTIFICATIONS,
);
if (result !== PermissionsAndroid.RESULTS.GRANTED) {
console.warn(
"Notifications permission not granted. User won't be able to see that the app is in background.",
);
}
} catch (err) {
console.warn(err);
}
};
const useForegroundServiceAndroid = ({
enableCamera,
enableMicrophone,
enableScreenSharing,
channelId,
channelName,
notificationContent,
notificationTitle,
importance,
}: ForegroundServiceConfig) => {
const [isConfigured, setIsConfigured] = useState(false);
useEffect(() => {
if (!isConfigured) {
return;
}
WebRTCModule.startForegroundService({
enableCamera,
enableMicrophone,
enableScreenSharing,
channelId,
channelName,
notificationContent,
notificationTitle,
importance,
}).catch(console.error);
}, [
channelId,
channelName,
enableCamera,
enableMicrophone,
enableScreenSharing,
isConfigured,
notificationContent,
notificationTitle,
importance,
]);
useEffect(() => {
const runConfiguration = async () => {
await requestNotificationsPermission();
setIsConfigured(true);
};
runConfiguration();
return () => {
WebRTCModule.stopForegroundService().catch(console.error);
};
}, []);
};
const emptyFunction = () => {};
export const useForegroundService = Platform.select({
android: useForegroundServiceAndroid,
default: emptyFunction,
});