-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathApp.txt
175 lines (154 loc) · 4.45 KB
/
App.txt
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
// TODO: Remove when not needed.
import React, {useState, useEffect} from 'react';
import {View} from 'react-native';
import {Button, Subheading} from 'react-native-paper';
import TextInput from 'react-native-paper/src/components/TextInput/TextInput';
import auth from '@react-native-firebase/auth';
import firestore from '@react-native-firebase/firestore';
import messaging from '@react-native-firebase/messaging';
import {BleManager} from 'react-native-ble-plx';
const requestNotificationPermission = () => {
messaging()
.requestPermission()
.then(() => {
return true;
// User has authorised
})
.catch(error => {
return false;
// User has rejected permissions
});
};
const setToken = async () => {
const fcmToken = await messaging().getToken();
if (fcmToken) {
// user has a device token
firestore()
.collection('temp')
.doc('nBPuzvtguV1UIYyxkkRo')
.update({fcmToken})
.then(() => alert('Added!'));
createNotificationListeners();
} else {
throw new Error('token not found.');
}
};
function createNotificationListeners() {
/*
* Triggered when a particular notification has been received in foreground
* */
messaging().onMessage(async remoteMessage => {
console.log(`A new FCM message arrived! ${JSON.stringify(remoteMessage)}`);
});
messaging().setBackgroundMessageHandler(async remoteMessage => {
console.log('Message handled in the background!', remoteMessage);
});
}
const App = () => {
const [confirm, setConfirm] = useState(null);
const [code, setCode] = useState('');
const [user, setLoggedInUser] = useState(null);
const [userName, setUserName] = useState(null);
const blManager = new BleManager();
useEffect(() => {
// if (user) {
// const enabled = messaging().hasPermission();
// if (enabled) {
// // user has permissions
// setToken();
// } else {
// // user doesn't have permission
// requestNotificationPermission();
// }
// }
const subscription = blManager.onStateChange(state => {
if (state === 'PoweredOn') {
scanAndConnect();
subscription.remove();
}
}, true);
});
function scanAndConnect() {
blManager.startDeviceScan(
null,
{allowDuplicates: false},
(error, device) => {
if (error) {
// Handle error (scanning will be stopped automatically)
console.log('ERR', error);
return;
}
if(device.txPowerLevel !== null){
console.log('FOUND', device);
}
// Check if it is a device you are looking for based on advertisement data
// or other criteria.
// if (device.name === 'TI BLE Sensor Tag' ||
// device.name === 'SensorTag') {
//
// // Stop scanning as it's not necessary if you are scanning for one device.
// blManager.stopDeviceScan();
//
// // Proceed with connection.
// }
},
);
}
// Handle the button press
async function signInWithPhoneNumber(phoneNumber) {
const confirmation = await auth().signInWithPhoneNumber(phoneNumber);
setConfirm(confirmation);
}
async function confirmCode() {
try {
const res = await confirm.confirm(code);
console.log('RES', res);
setLoggedInUser(res._user);
} catch (error) {
alert('Invalid code.');
console.log('Invalid code.');
}
}
const getUserName = async () => {
const tempDoc = await firestore()
.collection('temp')
.doc('nBPuzvtguV1UIYyxkkRo')
.get();
const {name} = tempDoc.data();
setUserName(name);
};
return (
<View>
{!confirm ? (
<Button
mode="contained"
onPress={() => signInWithPhoneNumber('+91 9158187707')}>
Sign in
</Button>
) : !user ? (
<>
<TextInput
style={{height: 40, border: '1px solid'}}
value={code}
onChangeText={text => setCode(text)}
/>
<Button mode="contained" onPress={() => confirmCode()}>
Confirm Code
</Button>
</>
) : (
<></>
)}
{user ? (
<>
<Subheading>Logged In. {user.phoneNumber}</Subheading>
<Button onPress={() => getUserName()}>Get User Name</Button>
</>
) : (
<></>
)}
{userName ? <Subheading>{userName}</Subheading> : null}
</View>
);
};
export default App;