This repository has been archived by the owner on Apr 9, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.js
190 lines (158 loc) · 5.17 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
import 'react-native-gesture-handler'
import React from 'react'
import {
Alert,
SafeAreaView,
StyleSheet,
ScrollView,
View,
Text,
StatusBar,
} from 'react-native'
import {
Icon,
} from 'react-native-elements'
import {
NavigationContainer
} from '@react-navigation/native'
import {
createBottomTabNavigator
} from '@react-navigation/bottom-tabs'
import {
createStackNavigator
} from 'react-navigation-stack'
import SplashScreen from 'react-native-splash-screen'
import AsyncStorage from '@react-native-community/async-storage'
import firebase from 'react-native-firebase'
//Stacks
import HomeStack from './app/stacks/HomeStack'
import TodayStack from './app/stacks/TodayStack'
import ExhibitListStack from './app/stacks/ExhibitListStack'
import DiscoverStack from './app/stacks/DiscoverStack'
import HomeScreen from './app/views/HomeScreen'
import AboutScreen from './app/views/AboutScreen'
import TodayAtUMMNHScreen from './app/views/TodayAtUMMNHScreen'
import ExhibitListScreen from './app/views/ExhibitListScreen'
import DiscoverScreen from './app/views/DiscoverScreen'
import GalleryScreen from './app/views/GalleryScreen'
import ExitScreen from './app/views/ExitScreen'
import EndOfTourScreen from './app/views/EndOfTourScreen'
import TourPreviewScreen from './app/views/TourPreviewScreen'
import colors from './app/modules/Colors'
const Tab = createBottomTabNavigator()
export default class App extends React.Component {
//When application loads, check for permissions and listen for notifications
componentDidMount = async () => {
SplashScreen.hide()
this.checkPermission()
this.createNotificationListeners()
}
//Remove listeners allocated in createNotificationListener
componentWillUnmount(){
this.notificationListener()
//this.notificationOpenedListener()
}
//Check if permission given
checkPermission = async () => {
const enabled = await firebase.messaging().hasPermission();
if (enabled) {
this.getToken();
} else {
this.requestPermission();
}
}
//Request permission if not granted
requestPermission = async () => {
try {
await firebase.messaging().requestPermission();
this.getToken()
} catch (error) {
console.log('permission rejected')
}
}
//Store Firebase token
getToken = async () => {
let fcmToken = null
try {
fcmToken = await AsyncStorage.getItem('fcmToken')
} catch (e) {
console.log('error trying to fetch fcm token (it likely does not exist)', e)
}
if(!fcmToken) {
console.log('fcm token is null')
fcmToken = await firebase.messaging().getToken();
if(fcmToken){
console.log('fcm token is no longer null')
try {
console.log('current fcm token', fcmToken)
await AsyncStorage.setItem('fcmToken', fcmToken)
} catch (e) {
console.log('could not save fcm token to async storage', e)
}
}
}
}
createNotificationListeners = async () => {
//Message received while app in foreground
this.notificationListener = firebase.notifications().onNotification((notification) => {
const { title, body } = notification
this.showAlert(title, body)
})
//Message received while app in background/tapped
// this.notificationOpenedListener = firebase.notifications().onNotificationOpened((notificationOpen) => {
// const {title, body } = notificationOpen.notification;
// this.showAlert(title, body)
// })
//Message received while app closed/tapped
// const notificationOpen = await firebase.notifications().getInitialNotification()
// if(notificationOpen){
// const { title, body } = notificationOpen.notification
// this.showAlert(title, body)
// }
//Data Only Message
this.messageListener = firebase.messaging().onMessage((message) => {
console.log(JSON.stringify(message))
})
}
showAlert(title, body){
Alert.alert(
title, body,
[
{ text: 'OK', onPress: () => console.log('OK Pressed')},
],
{ cancelable: false },
)
}
render() {
return (
<NavigationContainer>
<Tab.Navigator
screenOptions = {({route}) => ({
tabBarIcon: ({ focused, color, size }) => {
let iconName
if(route.name === 'Home') {
iconName = 'md-home'
} else if(route.name === 'Today'){
iconName = 'md-calendar'
} else if(route.name === 'Exhibits'){
iconName = 'md-list'
} else if(route.name === 'Discover'){
iconName = 'md-compass'
}
return <Icon type='ionicon' name = {iconName} color = {color}/>
}
})}
tabBarOptions = {{
activeTintColor: colors.ummnhLightBlue,
inactiveTintColor: 'gray'
}}
>
<Tab.Screen name = "Home" component = {HomeStack}/>
<Tab.Screen name = "Today" component = {TodayStack}/>
<Tab.Screen name = "Exhibits" component = {ExhibitListStack}/>
<Tab.Screen name = "Discover" component = {DiscoverStack}/>
</Tab.Navigator>
</NavigationContainer>
)
}
};