-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathProgressScreen.js
217 lines (200 loc) · 6.51 KB
/
ProgressScreen.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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
import React from 'react';
import {
Image,
Platform,
ScrollView,
StyleSheet,
Text,
TouchableOpacity,
View,
Dimensions,
Button
} from 'react-native';
import * as firebase from 'firebase';
import OneSignal from 'react-native-onesignal';
const FBSDK = require('react-native-fbsdk');
const {
AccessToken,
GraphRequest,
} = FBSDK;
const LATITUDE = 37.8715926;
const LONGITUDE = -122.27274699999998;
var getTestPath= function(userId){
return testpath = "/user/" + userId+ "/details/" + "/home/";
};
export default class ProgressScreen extends React.Component {
static navigationOptions = {
header: null,
};
constructor(props){
super(props);
}
state = {
position:{
latitude: LATITUDE,
longitude: LONGITUDE
},
DestinationMarker:{
latitude: this.props.navigation.state.params.latitude,
longitude: this.props.navigation.state.params.longitude
},
Friends:[],
OneSignalIdList: []
}
OneSignalIdList = []
FriendsUserList = []
GetFriendsSignalID = function(userId){
console.warn(userId);
var testpath = "/user/"+userId +"/details/";
firebase.database().ref(testpath).on("value", (snap) => {
id = snap.val().OneSignalId.ID;
name = snap.val().name
let data = [1,2] // some array as payload
let contents = {
'en': name+' has gotten home'
}
OneSignal.postNotification(contents, data, id);
this.props.navigation.navigate('CompleteJourneyScreen');
this.state.OneSignalIdList.push(id);
});
}
sendMessages = function(userId){
var testpath = "/user/"+userId +"/details/" + "/friends/"+"/myPack";
firebase.database().ref(testpath).on("value", (snap) => {
this.state.Friends = snap.val();
for (var friends in this.state.Friends){
console.warn(this.state.Friends[friends]);
this.GetFriendsSignalID(parseInt(this.state.Friends[friends]));
}
});
}
sendMessagesfromUsers(token){
fetch('https://graph.facebook.com/v2.5/me?fields=id&access_token=' + token)
.then((response) => response.json())
.then((json) => {
console.warn(json.id);
this.sendMessages(json.id);
})
}
componentDidMount() {
OneSignal.addEventListener('ids', this.onIds);
OneSignal.addEventListener('received', this.onReceived);
OneSignal.addEventListener('opened', this.onOpened);
OneSignal.addEventListener('registered', this.onRegistered);
OneSignal.addEventListener('ids', this.onIds);
navigator.geolocation.getCurrentPosition((position) => {
let initialPosition = JSON.stringify(position);
this.setState({ position });
}, error => console.log(error), { enableHighAccuracy: false, timeout: 20000, maximumAge: 1000 });
this.watchID = navigator.geolocation.watchPosition((position) => {
var latitude = parseInt(JSON.stringify(position.coords.latitude));
var longitude = parseInt(JSON.stringify(position.coords.longitude));
if (latitude > this.state.DestinationMarker.latitude-0.0001 && latitude<this.state.DestinationMarker.latitude+0.0001 && longitude> this.state.DestinationMarker.longitude-0.0001 && longitude<this.state.DestinationMarker.longitude+0.0001){
AccessToken.getCurrentAccessToken().then(
(data) => {
//Determine if this was their first login onto the app
const {accessToken} = data;
this.sendMessagesfromUsers(accessToken);
});
}
});
this.watchID = navigator.geolocation.watchPosition((position) => {
var latitude = parseInt(JSON.stringify(position.coords.latitude));
var longitude = parseInt(JSON.stringify(position.coords.longitude));
if (latitude > this.state.DestinationMarker.latitude-0.0001 && latitude<this.state.DestinationMarker.latitude+0.0001&& longitude> this.state.DestinationMarker.longitude-0.0001 && longitude<this.state.DestinationMarker.longitude+0.0001){
AccessToken.getCurrentAccessToken().then(
(data) => {
//Determine if this was their first login onto the app
const {accessToken} = data;
this.sendMessagesfromUsers(accessToken);
});
}
});
}
componentWillUnmount(){
OneSignal.removeEventListener('ids', this.onIds);
navigator.geolocation.clearWatch(this.watchID);
}
async ConfirmLogIn(navigate){
try{
await firebase.auth().onAuthStateChanged( function(user) {
if (user) { ;
} else {
console.log('xd');
navigate('Login');
}});
}
catch(error){}
};
onIds = (device) => {
console.log(device.userId);
}
onReceived(notification) {
console.log("Notification received: ", notification);
}
onOpened(openResult) {
console.log('Message: ', openResult.notification.payload.body);
console.log('Data: ', openResult.notification.payload.additionalData);
console.log('isActive: ', openResult.notification.isAppInFocus);
console.log('openResult: ', openResult);
}
onRegistered(notifData) {
console.log("Device had been registered for push notifications!", notifData);
}
render() {
const { params } = this.props.navigation.state;
const { navigate } = this.props.navigation;
this.ConfirmLogIn(navigate);
return (
<View style={styles.container}>
<Text style={styles.progressText}>Heading home...</Text>
<Image style = {styles.dingo}
source={require('./img/dingotransparent.png')} />
<TouchableOpacity onPress={() => this.props.navigation.navigate('HomeScreen')}>
<View style={styles.cancelButton}>
<Text style={styles.cancelText}>Cancel</Text>
</View>
</TouchableOpacity>
</View>
);
}
}
const window = Dimensions.get('window');
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#191979',
padding: 30,
paddingTop: 50,
alignItems: 'center',
},
progressText: {
marginTop: 50,
alignItems: 'center',
fontSize: 30,
fontFamily: 'Avenir',
color: '#fff',
},
dingo: {
marginTop: 40,
marginBottom: 20,
width: 82,
height: 122,
},
cancelButton: {
margin: 20,
padding: 10,
borderRadius: 30,
backgroundColor: '#FFFFFF',
width: window.width / 2,
alignItems: 'center',
shadowOffset: {width: 0, height: 4},
shadowOpacity: 0.25,
shadowRadius: 4,
shadowColor: '#000000',
},
cancelText: {
color: '#171797',
fontSize: 15
},
});