-
Notifications
You must be signed in to change notification settings - Fork 0
/
App.tsx
102 lines (94 loc) · 3.24 KB
/
App.tsx
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
import React, { useEffect, useState } from 'react';
import { NavigationContainer } from '@react-navigation/native';
import { createStackNavigator } from '@react-navigation/stack';
import AuthStackNavigator from './navigators/AuthStackNavigator';
import SplashScreen from './screens/splash/SplashScreen';
import DrawerNavigator from './navigators/drawer/DrawerNavigator';
import NetInfo from '@react-native-community/netinfo';
import { Platform } from 'react-native';
import Constants from 'expo-constants';
import * as Notifications from 'expo-notifications';
import * as Permissions from 'expo-permissions';
import Fire from './config/Fire';
//Stack contenant toute l'application
const RootStack = createStackNavigator();
export default function App() {
const [firstLaunch, setFirstLaunch] = useState(true);
const [loading, setLoading] = useState(true);
const [connected, setConnected] = useState(true);
useEffect(() => {
const unsubscribe = NetInfo.addEventListener(async (state) => {
if (firstLaunch == true) {
setFirstLaunch(false);
if (state.isConnected == true) {
Fire.shared.firebase.auth().onAuthStateChanged(async (user) => {
if(user){
setConnected(true)
if (Fire.shared.uid != undefined) {
await getTokenAndUpdate();
setLoading(false);
}
}else{
setConnected(false);
setLoading(false);
}
});
}
}
Fire.shared.connectedToInternet = state.isConnected;
});
return () => {
unsubscribe;
};
}, []);
const getTokenAndUpdate = async () => {
await registerForPushNotificationsAsync().then((token) => {
Fire.shared.updateToken(token);
});
};
const registerForPushNotificationsAsync = async () => {
console.log('registerForPushNotificationsAsync...');
let token;
if (Constants.isDevice) {
const { status: existingStatus } = await Permissions.getAsync(Permissions.NOTIFICATIONS);
let finalStatus = existingStatus;
if (existingStatus !== 'granted') {
const { status } = await Permissions.askAsync(Permissions.NOTIFICATIONS);
finalStatus = status;
}
if (finalStatus !== 'granted') {
alert('Failed to get push token for push notification!');
return;
}
token = (await Notifications.getExpoPushTokenAsync()).data;
} else {
alert('Must use physical device for Push Notifications');
}
if (Platform.OS === 'android') {
Notifications.setNotificationChannelAsync('default', {
name: 'default',
importance: Notifications.AndroidImportance.MAX,
vibrationPattern: [0, 250, 250, 250],
lightColor: '#FF231F7C',
});
}
return token;
};
return (
<NavigationContainer>
<RootStack.Navigator
screenOptions={{
headerShown: false,
}}
>
{loading ? (
<RootStack.Screen name={'Splash'} component={SplashScreen} />
) : connected ? (
<RootStack.Screen name={'AppStack'} component={DrawerNavigator} />
) : (
<RootStack.Screen name={'AuthStack'} component={AuthStackNavigator} />
)}
</RootStack.Navigator>
</NavigationContainer>
);
}