Skip to content

Commit 9f11b01

Browse files
committed
FCM inplementation
1 parent f39fd31 commit 9f11b01

15 files changed

+863
-80
lines changed

App.js

+93-44
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ import DeviceInfo from 'react-native-device-info';
1414
import { createStackNavigator, createSwitchNavigator, createAppContainer } from 'react-navigation';
1515
import ListScreen from './ListScreen';
1616
import AWSSignature from 'react-native-aws-signature';
17+
import Loader from './Loader';
18+
import WebviewScreen from './WebviewScreen';
19+
import axios from 'axios';
1720

1821
Platform.select({
1922
ios: 'Press Cmd+R to reload,\n' + 'Cmd+D or shake for dev menu',
@@ -31,21 +34,18 @@ class HomeScreen extends Component<Props> {
3134
username: '',
3235
password: '',
3336
loginStatus: false,
37+
loader: false
3438
};
3539
this.validate = this.validate.bind(this);
36-
37-
38-
const loginToken = AsyncStorage.getItem("loginToken");
39-
if(loginToken == "1"){
40+
this.validateGuid = this.validateGuid.bind(this);
41+
// AsyncStorage.clear(async function(){
4042

41-
this.setState({loginStatus: true});
42-
}else{
43-
this.setState({loginStatus: false});
44-
}
43+
//});
44+
4545
//Alert.alert(JSON.stringify(userToken));
4646
// This will switch to the App screen or Auth screen and this loading
4747
// screen will be unmounted and thrown away.
48-
this.props.navigation.navigate(this.state.loginStatus ? 'List' : 'App');
48+
4949

5050

5151

@@ -56,13 +56,52 @@ class HomeScreen extends Component<Props> {
5656
static navigationOptions = {
5757
header: null
5858
}
59+
60+
61+
validateGuid = async () => {
62+
var thisobj = this;
63+
var loginToken = await AsyncStorage.getItem("loginToken");
64+
await Alert.alert("login token "+JSON.stringify(loginToken));
65+
//console.log('login token', loginToken);
66+
if(loginToken != ""){
67+
DeviceInfo.getMACAddress().then(mac => {
68+
//Alert.alert("mac "+mac);
69+
70+
axios.get('https://mdev.humaxdigital.com/HumaxMobile/NonAuth/AuthMobileApp.aspx?AppID=Mobile&MacAddress='+mac+'&AuthKey=' +loginToken )
71+
.then(async function(responseData){
72+
await Alert.alert("login token "+responseData.data);
73+
//console.log('verify res', responseData);
74+
if(responseData.data){
75+
thisobj.props.navigation.navigate('List');
76+
}else {
77+
78+
AsyncStorage.removeItem("loginToken");
79+
thisobj.props.navigation.navigate('App');
80+
}
81+
});
82+
83+
// "E5:12:D8:E5:69:97"
84+
85+
});
86+
87+
88+
}else{
89+
90+
this.props.navigation.navigate('App');
91+
}
92+
}
5993
_bootstrapAsync = async () => {
94+
this.setState({
95+
loader:true
96+
})
6097
const dname = DeviceInfo.getDeviceName();
6198
//const mac = DeviceInfo.getMACAddress();
6299
const appName = DeviceInfo.getApplicationName();
63100
DeviceInfo.getMACAddress().then(mac => {
101+
102+
64103
// "E5:12:D8:E5:69:97"
65-
// console.log('mac', mac);
104+
console.log('mac', mac);
66105
var payload = {
67106
AppID: 'android',
68107
MacAddress: mac,
@@ -71,15 +110,17 @@ class HomeScreen extends Component<Props> {
71110
PASS: this.state.password,
72111
};
73112
var thisobj = this;
74-
fetch('https://mdev.humaxdigital.com/HumaxMobile/NonAuth/Login.aspx?AppID=Mobile&MacAddress='+mac+'&DeviceName='+appName+'&ID='+this.state.username+'&PASS='+this.state.password,{
75-
headers: {
76-
'Accept':'application/json'
77-
},
78-
}).then(async function(responseData){
113+
// fetch('https://mdev.humaxdigital.com/HumaxMobile/NonAuth/Login.aspx?AppID=Mobile&MacAddress='+mac+'&DeviceName='+appName+'&ID='+this.state.username+'&PASS='+this.state.password,{
114+
// headers: {
115+
// 'Accept':'application/json'
116+
// },
117+
// })
118+
axios.get('https://mdev.humaxdigital.com/HumaxMobile/NonAuth/Login.aspx?AppID=Mobile&MacAddress='+mac+'&DeviceName='+appName+'&ID='+this.state.username+'&PASS='+this.state.password )
119+
.then(async function(responseData){
79120

80-
var token = responseData._bodyInit._data.blobId;
121+
var token = responseData.data;
81122
let fcmToken = await AsyncStorage.getItem('fcmToken');
82-
console.log("first hit");
123+
console.log("guid", token);
83124
const path = '/production';
84125
const url = 'https://5q6xg017b6.execute-api.us-east-1.amazonaws.com'+ path;
85126
const credentials = {
@@ -119,59 +160,60 @@ class HomeScreen extends Component<Props> {
119160
method: 'POST',
120161
headers: auth_header,
121162
body: JSON.stringify({
163+
"username": thisobj.state.username,
122164
"tokenID": fcmToken,
123165
"guid": token,
124-
"deviceType": "android"
166+
"deviceType": "android",
167+
"deviceModel": appName,
168+
"macAddress": mac
169+
125170
})
126171
});
172+
127173
//const url = 'https://' + '5q6xg017b6.execute-api.us-east-1.amazonaws.com/production';
128-
174+
console.log(options);
129175
fetch(url, options).then(async function(resp){
130176
console.log("second hit");
131177
const responseData = await resp.json();
132-
await AsyncStorage.setItem('loginToken', "1");
178+
133179
await thisobj.setState({loginStatus: true});
134-
thisobj.props.navigation.navigate('List');
180+
//let resObj = JSON.stringify(responseData);
135181
//console.log("AWS "+ JSON.stringify(responseData));
182+
console.log(responseData.body);
183+
if(responseData.body == 'Success'){
184+
console.log('for setting instorage', token);
185+
await AsyncStorage.setItem('loginToken', token);
186+
var getting = await AsyncStorage.getItem('loginToken');
187+
console.log('from loacl storage', getting);
188+
thisobj.props.navigation.navigate('List');
189+
}
136190

137191
})
138192
//code end
139193
}
140194

141-
// res => {
142-
// var token = res._bodyInit._data.blobId;
143-
// let fcmToken = AsyncStorage.getItem('fcmToken');
144-
// // console.log('uid response', token);
145-
// var awspayload = {
146-
// tokenID: fcmToken,
147-
// guid: token,
148-
// deviceType: 'android'
149-
// };
150-
// //fetch('https://5q6xg017b6.execute-api.us-east-1.amazonaws.com/production')
195+
151196

152-
// }
197+
153198
)
154-
// console.log('paylod', payload);
155-
});
156199

157-
// console.log('device name', dname);
200+
});
158201

159-
// console.log('app name', appName);
160-
//const userToken = await AsyncStorage.getItem('userToken');
202+
161203

162204
};
163205
//On click of login btn
164206
onLogin() {
165207
if(this.validate()){
166208
const { username, password } = this.state;
167209
this._bootstrapAsync();
168-
// this.registerForPushNotificationsAsync();
169-
// Alert.alert('Credentials', `${username} + ${password}`);
170210
}
171211
else{
172212
Alert.alert('Both fields are required');
173213
}
174214
}
215+
216+
175217
//Validate input field
176218
validate() {
177219
const { username, password } = this.state;
@@ -182,18 +224,23 @@ validate() {
182224
return true;
183225
}
184226
}
185-
async componentDidMount(){
227+
async componentDidMount(){
228+
186229
this.checkLogin();
187230
this.checkPermission();
188231
this.createNotificationListeners();
189-
}
232+
233+
234+
}
190235

191236
////////////////////// Add these methods //////////////////////
192237

193238
//Remove listeners allocated in createNotificationListeners()
194239
componentWillUnmount() {
240+
195241
this.notificationListener();
196242
this.notificationOpenedListener();
243+
197244
}
198245

199246
async createNotificationListeners() {
@@ -286,13 +333,15 @@ showAlert(title, body) {
286333
async checkLogin() {
287334
let loginToken = await AsyncStorage.getItem('loginToken');
288335
if(loginToken){
336+
this.validateGuid();
289337
this.setState({loginStatus : true});
290338
}
291339
else{
292340
this.setState({loginStatus : false});
293341
}
294342
}
295343
render() {
344+
const {loader} = this.state;
296345
return (
297346
<View style={styles.main}>
298347
<Text style={styles.heading}>
@@ -318,7 +367,7 @@ showAlert(title, body) {
318367
onPress={this.onLogin.bind(this)}
319368
/>
320369
</View>
321-
370+
{loader && <Loader/>}
322371
</View>
323372
</View>
324373
);
@@ -380,8 +429,8 @@ const styles = StyleSheet.create({
380429
// marginBottom: 5,
381430
// },
382431
// });
383-
const ListStack = createStackNavigator({ List: ListScreen});
384-
const AppStack = createStackNavigator({ Home: HomeScreen })
432+
const ListStack = createStackNavigator({ List: ListScreen, UrlPage: WebviewScreen});
433+
const AppStack = createStackNavigator({ Home: HomeScreen });
385434
// const AuthStack = createStackNavigator({ SignIn: SignInScreen });
386435

387436
export default createAppContainer(createSwitchNavigator(

ListScreen.js

+45-36
Original file line numberDiff line numberDiff line change
@@ -1,66 +1,75 @@
11
import React from 'react';
2-
import {View, Text, StyleSheet, TouchableOpacity, Linking} from 'react-native';
2+
import {View, Text, StyleSheet, TouchableOpacity, Linking, WebView} from 'react-native';
33

44
// Dummy data to show in list
5-
const data = [
6-
{
7-
title : 'Title 1 goes here',
8-
desc : 'lorem ipsum sit dela dfgh qwjhjk',
9-
date : 'jan 2nd 2019',
10-
url : 'https://www.google.com/',
11-
},
12-
{
13-
title : 'Title 2 goes here',
14-
desc : 'lorem ipsum sit dela dfgh qwjhjk',
15-
date : 'jan 2nd 2019',
16-
url : 'https://www.google.com/',
17-
},
18-
{
19-
title : 'Title 3 goes here',
20-
desc : 'lorem ipsum sit dela dfgh qwjhjk',
21-
date : 'jan 2nd 2019',
22-
url : 'https://www.google.com/',
23-
},
24-
{
25-
title : 'Title 4 goes here',
26-
desc : 'lorem ipsum sit dela dfgh qwjhjk',
27-
date : 'jan 2nd 2019',
28-
url : 'https://www.google.com/',
29-
},
30-
31-
];
325

336
export default class ListScreen extends React.Component {
347
constructor(props) {
358
super(props);
36-
this.showList = this.showList.bind(this);
9+
this.state ={
10+
data : []
11+
}
12+
this.showList = this.showList.bind(this);
13+
this._onPressButton = this._onPressButton.bind(this);
14+
}
15+
componentDidMount(){
16+
var thisobj = this;
17+
fetch('https://phpq09tvz7.execute-api.us-east-1.amazonaws.com/prod',{
18+
headers: {
19+
'Accept':'application/json'
20+
},
21+
}).then(async function(resp){
22+
const responseData = await resp.json();
23+
console.log('msg list'+JSON.stringify(responseData.body));
24+
var temp = JSON.stringify(responseData.body.Items);
25+
var parseData = JSON.parse(temp);
26+
thisobj.setState({data:parseData});
27+
console.log('response data'+ JSON.parse(temp));
28+
});
29+
}
30+
_onPressButton(parm){
31+
this.props.navigation.navigate(
32+
'UrlPage',
33+
{ parm },
34+
);
3735
}
38-
3936
showList() {
40-
return data.map(function(list, i){
37+
console.log('showlist called', this.state.data);
38+
var data = this.state.data;
39+
let a = [];
40+
console.log('type of', typeof data);
41+
console.log('type of', typeof a);
42+
if(data.length>0){
43+
return data.map((list, i) => {
44+
//<TouchableOpacity onPress={()=>{Linking.openURL('https://'+list.message.S)}} style={styles.touchable}></TouchableOpacity>
4145
return(
4246
<View key={i} style={styles.list}>
43-
<TouchableOpacity onPress={()=>{Linking.openURL(list.url)}} style={styles.touchable}>
47+
<TouchableOpacity onPress={() => {this._onPressButton(list.message.S)}} style={styles.touchable}>
4448
<View style={styles.title}>
45-
<Text style={styles.heading}>{list.title}</Text>
46-
<Text style={styles.subhead}>{list.desc}</Text>
49+
<Text style={styles.heading}>{list.message.S}</Text>
50+
<Text style={styles.subhead}>{list.createdAt.S}</Text>
4751
</View>
4852
<View style={styles.dateSection}>
4953
<Text style={styles.subhead}>
50-
{list.date}
54+
5155
</Text>
5256
</View>
53-
</TouchableOpacity>
57+
</TouchableOpacity>
5458
</View>
5559
);
5660
});
5761
}
62+
63+
}
5864

5965
render() {
6066
return (
67+
6168
<View style={styles.main}>
6269
{this.showList()}
6370
</View>
71+
72+
6473
)}
6574
}
6675

0 commit comments

Comments
 (0)