-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathApp.js
More file actions
171 lines (131 loc) · 4.72 KB
/
Copy pathApp.js
File metadata and controls
171 lines (131 loc) · 4.72 KB
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
import React, { useEffect, useRef, useState } from 'react';
import { Text, View, SafeAreaView, Button, TextInput, Animated, TouchableOpacity } from 'react-native';
import { AntDesign } from '@expo/vector-icons'
import * as Permissions from 'expo-permissions'
import * as Notifications from 'expo-notifications'
//simplesmente o cabeçalho
import Cabecalho from './components/Cabecalho/index'
//o arquivo que contem o style sheet
import estilos from './estilos'
Notifications.setNotificationHandler({
handleNotification: async () => ({
shouldShowAlert: true,
shouldPlaySound: false,
shouldSetBadge: false,
}),
})
export default function App() {
const [temPermissaoNotificacoes, setTemPermissaoNotificacoes] = useState(null)
const [titulo, setTitulo] = useState('')
const [texto, setTexto] = useState('')
const listenerDeNotificacao = useRef()
const listenerDeResposta = useRef()
const mostrarAlert = useRef(new Animated.Value(0)).current;
const mostrar = () => {
Animated.timing(mostrarAlert, {
toValue: 1,
duration: 2000,
useNativeDriver: false
}).start()
};
const esconder = () => {
Animated.timing(mostrarAlert, {
toValue: 0,
duration: 2000,
useNativeDriver: false
}).start()
limparInputs();
};
const limparInputs = () => {
setTitulo('');
setTexto('');
};
useEffect(() => {
//pede permissão para gerar notificações
(
async () => {
const { status } = await Notifications.requestPermissionsAsync()
setTemPermissaoNotificacoes(status === 'granted')
}
)() //as vezes o app da um aviso relacionado a promessas
listenerDeNotificacao.current = Notifications.addNotificationReceivedListener()
listenerDeResposta.current = Notifications.addNotificationResponseReceivedListener()
Notifications.setNotificationChannelAsync('default', {
name: 'default',
importance: Notifications.AndroidImportance.MAX,
vibrationPattern: [0, 250, 250, 250],
lightColor: '#FF231F7C',
});
//o return do use effect é executado quando o elemento deixa de ser renderizado
return () => {
Notifications.removeNotificationSubscription(listenerDeNotificacao)
Notifications.removeNotificationSubscription(listenerDeResposta)
}
}, [])
//agenda uma notifcação
async function gerarNotificacao() {
await Notifications.scheduleNotificationAsync({
content: {
title: titulo.toString(),
body: texto.toString(),
},
trigger: { seconds: 1 },
//teoricamente daria pra mudar esse valor e fazer a Notif. aparecer em um certo horario
//mas eu não sei como isso iria interagir com o app sendo fechado
})
mostrar(); //mostra a seta da notificação
}
//É renderizado caso não tenha obtido permição para usar notificações
if (!temPermissaoNotificacoes) {
return <View><Text>Não foi possivel acessar as notificações</Text></View>
}
return (
<SafeAreaView style={estilos.container}>
{/*O Cabeçalho */}
<Cabecalho titulo='PushMe' />
<Animated.View
style={[{ opacity: mostrarAlert }, estilos.alert]}>
<TouchableOpacity onPress={esconder}>
<Text style={estilos.Talert}>Veja seu lembrete 👆!</Text>
</TouchableOpacity>
</Animated.View>
{/*O texto que explica o que o app faz */}
<View style={estilos.centro}>
<Text>Dê um título e escreva uma mensagem, pressione o botão</Text>{/*<br/> não funciona */}
<Text>e gere um lembrete em suas notificações!</Text>
</View>
<View style={estilos.centro}>
{/*O campo que segura o Titulo da notificação */}
<View style={estilos.alto}>
<Text style={estilos.texto}>Titulo:</Text>
<TextInput
onChangeText={texto => setTitulo(texto)}
defaultValue={titulo}
style={estilos.input}
placeholder='O Titulo Da Sua Notificação'
maxLength={30}
/>
</View>
{/*O campo que segura o corpo da notificação */}
<View style={estilos.alto}>
<Text style={estilos.texto}>Texto:</Text>
<TextInput
onChangeText={texto => setTexto(texto)}
defaultValue={texto}
style={estilos.input}
placeholder='O Texto Da Sua Notificação'
maxLength={100}
multiline={true}
/>
</View>
</View>
<View style={estilos.baixo}>
<TouchableOpacity onPress={async () => await gerarNotificacao()}>
<View>
<AntDesign name='pushpin' size={40} color={'#017e'} />
</View>
</TouchableOpacity>
</View>
</SafeAreaView >
);
}