-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.js
120 lines (113 loc) · 2.71 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
import React from 'react';
import {
StyleSheet,
Text,
View,
TextInput,
Modal,
TouchableOpacity
} from 'react-native';
import { Notifications } from 'expo';
import * as Permissions from 'expo-permissions';
import * as Device from 'expo-device';
const PUSH_REGISTRATION_ENDPOINT = 'http://d5745282d85b.ngrok.io/token';
const MESSAGE_ENPOINT = 'http://d5745282d85b.ngrok.io/message';
export default class App extends React.Component {
state = {
notification: null,
messageText: ''
}
componentDidMount() {
this.registerForPushNotificationsAsync();
}
registerForPushNotificationsAsync = async () => {
const { status } = await Permissions.askAsync(Permissions.NOTIFICATIONS);
if (status !== 'granted') {
return;
}
let token = await Notifications.getExpoPushTokenAsync();
return fetch(PUSH_REGISTRATION_ENDPOINT, {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
},
body: JSON.stringify({
token: {
value: token,
},
user: {
username: 'warly', //임의값
name: 'Dan Ward' //임의값
},
}),
});
this.notificationSubscription = Notifications.addListener(this.handleNotification);
}
handleNotification = (notification) => {
this.setState({ notification });
}
handleChangeText = (text) => {
this.setState({ messageText: text });
}
sendMessage = async () => {
fetch(MESSAGE_ENPOINT, {
method: 'POST',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
},
body: JSON.stringify({
message: this.state.messageText,
}),
});
this.setState({ messageText: '' });
}
render() {
return (
<View style={styles.container}>
<TextInput
value={this.state.messageText}
onChangeText={this.handleChangeText}
style={styles.textInput}
/>
<TouchableOpacity
style={styles.button}
onPress={this.sendMessage}
>
<Text style={styles.buttonText}>Send</Text>
<Text>{Device.modelName}</Text>
</TouchableOpacity>
{this.state.notification ?
this.renderNotification()
: null}
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#474747',
alignItems: 'center',
justifyContent: 'center',
},
textInput: {
height: 50,
width: 300,
borderColor: '#f6f6f6',
borderWidth: 1,
backgroundColor: '#fff',
padding: 10
},
button: {
padding: 10
},
buttonText: {
fontSize: 18,
color: '#fff'
},
label: {
fontSize: 18
}
});