import 'react-native-gesture-handler'; import * as React from 'react'; import { Platform, AppState, Text, TextInput, Alert, LogBox, BackHandler, View, Modal, StyleSheet } from 'react-native'; import { Provider } from "react-redux"; import firebase from '@react-native-firebase/app'; import '@react-native-firebase/messaging'; import { NativeBaseProvider } from 'native-base'; import Router from './screens/route.js'; import store from './redux/store'; import DeviceInfo from 'react-native-device-info'; import DB from './components/storage/'; import environment from './components/environment.js'; import axios from 'axios'; import { setJSExceptionHandler } from 'react-native-exception-handler'; import { callLogs } from './utils/logs.js'; var PushNotification = require("react-native-push-notification"); import JailMonkey from 'jail-monkey'; setJSExceptionHandler(async (error) => { callLogs(error, "error"); }); export default class App extends React.Component { constructor(props) { super(props) } _isMounted = false state = { appState: AppState.currentState, backgroundCaptureTime: null, deviceSecureModal: false, } appStateSubscription = null; checkDeviceSecurity = () => { const isJailBroken = JailMonkey.isJailBroken(); const isDeviceSecure = !isJailBroken; this.setState({ deviceSecureModal: !isDeviceSecure }); if (!isDeviceSecure && Platform.OS === 'android') { setTimeout(() => { BackHandler.exitApp(); }, 5000); } }; componentDidMount() { LogBox.ignoreAllLogs(); Text.defaultProps = {}; Text.defaultProps.allowFontScaling = false; TextInput.defaultProps = {}; TextInput.defaultProps = { ...(TextInput.defaultProps || {}), allowFontScaling: false }; this._isMounted = true; this.checkDeviceSecurity(); if (!this.state.deviceSecureModal) { this.saveDeviceUUID(); this.notificationAuthorization(); this.createNotificationListener(); // this.appVersionChecker() } // Updated AppState event listener this.appStateSubscription = AppState.addEventListener('change', this._handleAppStateChange); } componentWillUnmount() { this._isMounted = false; try { this.messageListener(); this.notificationOpenedListener(); this.notificationListener(); // Updated AppState event removal if (this.appStateSubscription) { this.appStateSubscription.remove(); } } catch (error) { } } _handleAppStateChange = (nextAppState) => { if (this.state.appState.match(/inactive|background/) && nextAppState === 'active') { // Re-check security when app comes back to foreground this.checkDeviceSecurity(); } else { this.setState({ appState: nextAppState }); } } saveDeviceUUID = () => { DB.set("deviceUUID", DeviceInfo.getUniqueId(), () => { }, (e) => console.log("DEVICE INFO SAVING FAILED!", e)) } appVersionChecker = async () => { const deviceVersion = DeviceInfo.getReadableVersion(); const version = await DB.get("deviceVersion"); if (!version) { await DB.set( "deviceVersion", deviceVersion, () => { }, () => { } ); } else { if (deviceVersion !== version) { await DB.reset(); await DB.set( "deviceVersion", deviceVersion, () => { }, () => { } ); if (Platform.OS === "ios") { Alert.alert("Information", "The existing app version will be uninstalled before proceeding with the upgrade to ensure a more stable and secure operation.") } else { Alert.alert("Information", "All local caches and data for this app will be refreshed to ensure a more stable and secure operation for this version and onwards.") } } } } notificationAuthorization = async () => { const authStatus = await firebase.messaging().requestPermission(); const enabled = authStatus === firebase.messaging.AuthorizationStatus.AUTHORIZED || authStatus === firebase.messaging.AuthorizationStatus.PROVISIONAL; if (enabled) { try { const token = await firebase.messaging().getToken() if (token) { let existingToken = await DB.get("fcmToken") || "" if (token != existingToken) { DB.set("fcmRegistration", "new", (r) => { }, (e) => { }) DB.set("fcmToken", token, () => { }) } } } catch (error) { console.log(error); } } } createNotificationListener = () => { this.messageListener = firebase.messaging().onMessage(message => { const { notification } = message PushNotification.localNotification({ title: notification.title || "Unioil Loyalty App", message: notification.body, playSound: false, soundName: "default" }) }); /* * Triggered when a particular notification has been received in foreground * */ this.notificationListener = firebase.messaging().onMessage(async remoteMessage => { console.log(remoteMessage) }); /* * If your app is in background, you can listen for when a notification is clicked / tapped / opened as follows: * */ this.notificationOpenedListener = firebase.messaging().onNotificationOpenedApp(remoteMessage => { console.log(remoteMessage) }); firebase.messaging() .getInitialNotification() .then(async remoteMessage => { if (remoteMessage) { if (Platform.OS == 'ios') { let result = await DB.AddNotification({ messageId: remoteMessage.data.from, title: remoteMessage.data.notification.title, body: remoteMessage.data.notification.body, visible: true, delivery: false, recieved: remoteMessage.data.from }) } else { let result = await DB.AddNotification({ messageId: remoteMessage.messageId, title: remoteMessage.notification.title, body: remoteMessage.notification.body, visible: true, delivery: false, recieved: remoteMessage.sentTime }) } } }); } renderBlockedDeviceModal = () => { return ( Security Warning This app cannot run on rooted devices. ); } render() { return ( {this.renderBlockedDeviceModal()} {!this.state.deviceSecureModal ? ( ) : ( )} ) } } const styles = StyleSheet.create({ blocked: { flex: 1, backgroundColor: '#fff', }, modalContainer: { flex: 1, backgroundColor: 'rgba(0,0,0,0.6)', justifyContent: 'center', alignItems: 'center' }, modalContent: { backgroundColor: 'white', padding: 24, borderRadius: 12, alignItems: 'center', marginHorizontal: 30 }, modalTitle: { fontSize: 18, fontWeight: 'bold', marginBottom: 12, textAlign: 'center' }, modalText: { fontSize: 15, textAlign: 'center', color: '#333' } });