189 lines
5.5 KiB
JavaScript
189 lines
5.5 KiB
JavaScript
|
|
import 'react-native-gesture-handler';
|
|
import * as React from 'react';
|
|
import { Platform, AppState, Text, TextInput, Alert, LogBox } 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");
|
|
|
|
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
|
|
}
|
|
|
|
async componentDidMount() {
|
|
LogBox.ignoreAllLogs();
|
|
Text.defaultProps = {};
|
|
Text.defaultProps.allowFontScaling = false;
|
|
|
|
TextInput.defaultProps = {};
|
|
TextInput.defaultProps = { ...(TextInput.defaultProps || {}), allowFontScaling: false };
|
|
|
|
this._isMounted = true
|
|
this.saveDeviceUUID()
|
|
this.notificationAuthorization()
|
|
this.createNotificationListener()
|
|
// this.appVersionChecker()
|
|
AppState.addEventListener('change', this._handleAppStateChange)
|
|
}
|
|
|
|
componentWillUnmount() {
|
|
this._isMounted = false
|
|
try {
|
|
this.messageListener()
|
|
this.notificationOpenedListener()
|
|
this.notificationListener()
|
|
AppState.removeEventListener('change', this._handleAppStateChange)
|
|
} catch (error) {}
|
|
}
|
|
|
|
_handleAppStateChange = (nextAppState) => {
|
|
if(this.state.appState.match(/inactive|background/) && nextAppState === 'active') {
|
|
|
|
} else {
|
|
|
|
}
|
|
}
|
|
|
|
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
|
|
})
|
|
}
|
|
|
|
}
|
|
});
|
|
}
|
|
|
|
render() {
|
|
return (
|
|
<Provider store={store}>
|
|
<NativeBaseProvider>
|
|
<Router />
|
|
</NativeBaseProvider>
|
|
</Provider>
|
|
)
|
|
}
|
|
} |