-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.js
196 lines (185 loc) · 5.77 KB
/
App.js
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
import React, {useEffect, useState} from 'react';
import reactotron from 'reactotron-react-native';
import MainNavigator from './src/navigation/MainNavigator';
import {I18nextProvider} from 'react-i18next';
import i18n from './src/utils/i18n';
import {
PermissionsAndroid,
Platform,
SafeAreaView,
StyleSheet,
Text,
} from 'react-native';
import {fcmService} from './src/utils/fcmServices';
import {localNotificationService} from './src/utils/localPushNotification';
import PushNotification from 'react-native-push-notification';
import {androidPlatform} from './src/utils/config';
export const AppContext = React.createContext(initialState);
const LOCAL_NOTIFICATION_CHANNEL_ID = 'high_priority_alerts';
let notificationIDs = 0;
const initialState = {
isAudioEnabled: true,
status: 'disconnected',
participants: new Map(),
videoTracks: new Map(),
userName: '',
roomName: '',
token: '',
isVideoEnabled: true,
};
function onNotification(notify) {
const options = {
playSound: true,
};
if (androidPlatform) {
if (Platform.Version > 25) {
PushNotification.getChannels(channelIDs => {
if (
channelIDs &&
channelIDs.length > 0 &&
channelIDs.includes(LOCAL_NOTIFICATION_CHANNEL_ID)
) {
notificationIDs += 1;
localNotificationService.showNotification(
notificationIDs,
notify.notification?.title
? notify.notification?.title
: notify?.data?.title,
notify.notification?.body
? notify.notification?.body
: notify?.data?.body,
notify?.data,
options,
LOCAL_NOTIFICATION_CHANNEL_ID,
);
} else {
PushNotification.createChannel(
{
channelId: LOCAL_NOTIFICATION_CHANNEL_ID, // (required)
channelName: 'Mentalica Application', // (required)
importance: 3,
},
created => {
notificationIDs += 1;
if (created) {
localNotificationService.showNotification(
notificationIDs,
notify.notification?.title
? notify.notification?.title
: notify?.data?.title,
notify.notification?.body
? notify.notification?.body
: notify?.data?.body,
notify?.data,
options,
LOCAL_NOTIFICATION_CHANNEL_ID,
);
}
},
);
}
});
} else {
notificationIDs += 1;
localNotificationService.showNotification(
notificationIDs,
notify.notification?.title
? notify.notification?.title
: notify?.data?.title,
notify.notification?.body
? notify.notification?.body
: notify?.data?.body,
notify?.data,
options,
);
}
} else {
localNotificationService.showNotification(
0,
notify.notification?.title
? notify.notification?.title
: notify?.data?.title,
notify.notification?.body
? notify.notification?.body
: notify?.data?.body,
notify?.data,
options,
);
}
}
function onOpenNotification(data) {}
function onRegister(token) {}
const registerNotification = () => {
fcmService.register(onRegister, onNotification, onOpenNotification);
localNotificationService.configure(onOpenNotification);
};
const App = () => {
const [props, setProps] = useState(initialState);
useEffect(() => {
if (androidPlatform) {
const requestPushNotificationPermission = async () => {
try {
if (Platform.Version > 32) {
const granted = await PermissionsAndroid.request(
PermissionsAndroid.PERMISSIONS.POST_NOTIFICATIONS,
{
title: 'Push Notification Permission',
message: 'This app requires access for push notification.',
buttonNeutral: 'Ask Me Later',
buttonNegative: 'Cancel',
buttonPositive: 'OK',
},
);
if (granted === PermissionsAndroid.RESULTS.GRANTED) {
Platform.OS == 'android' && registerNotification();
} else {
console.log('please allow notification permission from settings');
}
} else {
Platform.OS == 'android' && registerNotification();
}
} catch (err) {
console.error('errror --> ', err);
return false;
}
};
Platform.OS == 'android' && requestPushNotificationPermission();
} else {
Platform.OS == 'android' && fcmService.registerAppWithFCM();
Platform.OS == 'android' &&
fcmService.register(onRegister, onNotification, onOpenNotification);
Platform.OS == 'android' &&
localNotificationService.configure(onOpenNotification);
}
return () => {
Platform.OS == 'android' && fcmService.unRegister();
Platform.OS == 'android' && localNotificationService.unregister();
};
}, []);
if (__DEV__) {
const yeOldeConsoleLog = console.log;
console.log = (...args) => {
yeOldeConsoleLog(...args);
reactotron.display({
name: 'CONSOLE.LOG',
value: args,
preview:
args.length > 0 && typeof args[0] === 'string' ? args[0] : null,
});
};
}
return (
<AppContext.Provider value={{props, setProps}}>
<I18nextProvider i18n={i18n}>
<SafeAreaView style={styles.safeAreaViewStyle} />
<MainNavigator style={styles.mainNavigator} />
</I18nextProvider>
</AppContext.Provider>
);
};
export default App;
const styles = StyleSheet.create({
safeAreaViewStyle: {
flex: 0,
},
});