import { StatusBar } from ‘expo-status-bar’;
import Constants from ‘expo-constants’;
import * as Notifications from ‘expo-notifications’;
import React, { useState, useEffect, useRef } from ‘react’;
import { Text, View, Button, Platform,LogBox,ActivityIndicator,StyleSheet,BackHandler,Alert} from ‘react-native’;
import { WebView } from ‘react-native-webview’;
Notifications.setNotificationHandler({
handleNotification: async () => ({
shouldShowAlert: true,
shouldPlaySound: false,
shouldSetBadge: false,
}),
});
export default function App() {
const [expoPushToken, setExpoPushToken] = useState(‘’);
const [notification, setNotification] = useState(false);
const notificationListener = useRef();
const responseListener = useRef();
LogBox.ignoreAllLogs(true);
const [reca,setReca] = useState(true);
const recarregar = () =>{
setReca(!reca);
}
const[visible,setVisible] = useState(true);
hideSpinner=()=> {
setVisible(false);
}
showSpinner=()=> {
setVisible(true);
}
useEffect(() => {
const backAction = () => {//botao de voltar
Alert.alert('Alerta','Você deseja fechar o App?', [
{
text: 'Cancelar',
onPress: () => null,
style: 'cancel',
},
{ text: 'Sim', onPress: () => BackHandler.exitApp() },
]);
return true;
};
const backHandler = BackHandler.addEventListener('hardwareBackPress', backAction);
//passo 1 : token
registerForPushNotificationsAsync().then(token => setExpoPushToken(token));
//terceiro passo
notificationListener.current = Notifications.addNotificationReceivedListener(notification => {
setNotification(notification);
});
//terceiro passo
responseListener.current = Notifications.addNotificationResponseReceivedListener(response => {
console.log(response);
});
return () => {
Notifications.removeNotificationSubscription(notificationListener.current);
Notifications.removeNotificationSubscription(responseListener.current);
backHandler.remove();
};
}, );
return (
{
(reca)?
< WebView
onLoadStart={() => (showSpinner())}
onLoadEnd={() => hideSpinner()}
source = { { uri : 'https://treinamento.viashopmoda.com.br/?origem=app'} }
renderError={() =>
<View style={styles.loadingOrErrorView}>
<View style={styles.container}>
<Text style={{fontSize:25,marginBottom:25,color:'#BEBEBE',textAlign:'center'}}>Não foi possível conectar-se a internet</Text>
<TouchableOpacity style={styles.botao} onPress={()=>recarregar()}>
<Text style={{color:'#BEBEBE',fontSize:18,textAlign:'center'}}>Repetir</Text>
</TouchableOpacity>
</View>
</View>
}
/>
:
<View style={{flex:1}}>
< WebView
onLoadStart={() => (showSpinner())}
onLoad={() => hideSpinner()}
source = { { uri : 'https://treinamento.viashopmoda.com.br/?origem=app' } }
renderError={() =>
<View style={styles.loadingOrErrorView}>
<View style={styles.container}>
<Text style={{fontSize:25,marginBottom:25,color:'#BEBEBE',textAlign:'center'}}>Não foi possível conectar-se a internet</Text>
<TouchableOpacity style={styles.botao} onPress={()=>recarregar()}>
<Text style={{color:'#BEBEBE',fontSize:18,textAlign:'center'}}>Repetir</Text>
</TouchableOpacity>
</View>
</View>
}
/>
</View>
}
{
(visible)?
<ActivityIndicator style={styles.telac} color="gray" size="large"></ActivityIndicator>
:
<View></View>
}
);
}
const styles = StyleSheet.create({
container: {
height:'100%',
width:'100%',
flex: 1,
justifyContent: 'center',
backgroundColor: 'white',
alignItems:'center'
},
botao:{
borderWidth: 1,
borderColor: "#BEBEBE",
padding:5,
paddingLeft:22,
paddingRight:22,
borderRadius:20
},
loadingOrErrorView:{
position:'absolute',
flex:1,
justifyContent:'center',
alignItems:'center',
height:'100%',
width:'100%',
backgroundColor:'white'
},
telac:{
flex: 1,
left: 0,
right: 0,
top: 0,
bottom: 0,
position: 'absolute',
alignItems: 'center',
justifyContent: 'center'
},
});
async function schedulePushNotification() {//versao local
await Notifications.scheduleNotificationAsync({
content: {
title: "Você recebeu um email 📬",
body: 'Voce tem uma promoção ',
data: { data: 'goes here' },
},
trigger: { seconds: 2 },
});
}
async function registerForPushNotificationsAsync() {
let token;
if (Constants.isDevice) {
const { status: existingStatus } = await Notifications.getPermissionsAsync();
let finalStatus = existingStatus;
if (existingStatus !== ‘granted’) {
const { status } = await Notifications.requestPermissionsAsync();
finalStatus = status;
}
if (finalStatus !== ‘granted’) {
alert(‘Failed to get push token for push notification!’);
return;
}
token = (await Notifications.getExpoPushTokenAsync()).data;
console.log(token);
} else {
alert(‘Must use physical device for Push Notifications’);
}
if (Platform.OS === ‘android’) {
Notifications.setNotificationChannelAsync(‘default’, {
name: ‘default’,
importance: Notifications.AndroidImportance.MAX,
vibrationPattern: [0, 250, 250, 250],
lightColor: ‘#FF231F7C’,
});
}
return token;
}