-
Notifications
You must be signed in to change notification settings - Fork 0
/
App.js
188 lines (165 loc) · 6.91 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
import * as Notifications from 'expo-notifications';
import * as Permissions from 'expo-permissions';
import * as Updates from 'expo-updates';
import * as eva from '@eva-design/eva';
import {AsyncStorage, StatusBar} from 'react-native';
import React, {Component} from 'react';
import AboutMeScreen from './src/Screens/AboutMeScreen';
import {AppContext} from './src/Contexts/AppContext';
import {ApplicationProvider} from '@ui-kitten/components';
import Bugsnag from '@bugsnag/expo';
import CategoryDetailScreen from './src/Screens/CategoryDetailScreen';
import CategoryScreen from './src/Screens/CategoryScreen';
import Constants from 'expo-constants';
import HomeScreen from './src/Screens/HomeScreen';
import {NavigationContainer} from '@react-navigation/native';
import NavigatorService from './src/Services/NavigatorService';
import NotificationUtils from "./src/Utils/NotificationUtils";
import QuestionScreen from './src/Screens/QuestionScreen';
import {SafeAreaProvider} from 'react-native-safe-area-context';
import Spinner from 'react-native-loading-spinner-overlay';
import {createStackNavigator} from '@react-navigation/stack';
import firebase from './src/DataStorages/FirebaseApp';
const Stack = createStackNavigator();
Notifications.setNotificationHandler({
handleNotification: async () => ({
shouldShowAlert: true,
shouldPlaySound: false,
shouldSetBadge: false,
}),
});
export default class App extends Component {
state = {
isReady: false,
loading: false,
setLoading: (loading) => {
if (this.state.loading !== loading) {
this.setState({loading});
}
}
};
async checkNewCode() {
try {
if (!__DEV__) {
const update = await Updates.checkForUpdateAsync();
if (update.isAvailable) {
await Updates.fetchUpdateAsync();
await Updates.reloadAsync()
.then(value => {
this.setState({
loading: false
})
})
}
}
} catch (e) {
// handle or log error
console.log(e);
this.setState({
loading: false
})
}
}
async componentDidMount() {
await Bugsnag.start();
await this.checkNewCode();
const notificationListener = Notifications.addNotificationReceivedListener(notification => {
setNotification(notification);
});
const responseListener = Notifications.addNotificationResponseReceivedListener(response => {
console.log(response);
});
Notifications.removeNotificationSubscription(notificationListener);
Notifications.removeNotificationSubscription(responseListener);
this.setState({...this.state, isReady: true});
this.anonymousFirebaseLogin();
}
anonymousFirebaseLogin() {
firebase.auth().signInAnonymously()
.then((auth) => {
this.setState({...this.state, isReady: true}, () => {
this.registerForPushNotificationsAsync().then(token => {
if (token) {
firebase.database().ref(`users/${auth.user.uid}`)
.set({
expoPushToken: token
})
}
});
});
})
.catch((error) => {
var errorCode = error.code;
var errorMessage = error.message;
Alert.alert(`Xảy ra lỗi', 'Đã xảy ra lỗi khi xác thực người dùng, mã lỗi : ${errorCode} - ${errorMessage}`);
console.error(error);
setTimeout(() => {
this.anonymousFirebaseLogin()
}, 5000)
});
}
async 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') {
console.log('Failed to get push token for push notification!');
return;
}
token = (await Notifications.getExpoPushTokenAsync()).data;
console.log(token);
} else {
console.log('Must use physical device for Push Notifications');
}
if (Platform.OS === 'android') {
await Notifications.setNotificationChannelAsync('default', {
name: 'default',
importance: Notifications.AndroidImportance.MAX,
vibrationPattern: [0, 250, 250, 250],
lightColor: '#FF231F7C',
});
}
await AsyncStorage.getItem('user_reminder')
.then((rawData) => {
if (rawData === null) {
const selectedDayInWeek = [1, 2, 4, 5];
const reminderTime = 38700000;
NotificationUtils.SaveAndUpdateReminderData(selectedDayInWeek, reminderTime);
}
})
return token;
}
render() {
return (
<ApplicationProvider {...eva} theme={eva.light}>
<SafeAreaProvider>
<AppContext.Provider value={this.state}>
<NavigationContainer ref={(el) => NavigatorService.setContainer(el)}>
<Stack.Navigator initialRouteName="Home">
<Stack.Screen name="Home" component={HomeScreen} options={{headerShown: false}}/>
<Stack.Screen name="CategoryScreen" component={CategoryScreen}/>
<Stack.Screen name="CategoryDetailScreen" component={CategoryDetailScreen}/>
<Stack.Screen name="QuestionScreen" component={QuestionScreen}/>
<Stack.Screen name="AboutMeScreen" component={AboutMeScreen}/>
</Stack.Navigator>
</NavigationContainer>
<StatusBar
barStyle="dark-content"
/>
<Spinner
visible={this.state.loading}
textStyle={{color: '#fff'}}
cancelable={true}
textContent={'Đang Tải...'}
/>
</AppContext.Provider>
</SafeAreaProvider>
</ApplicationProvider>
);
}
}