89 lines
3.0 KiB
JavaScript
89 lines
3.0 KiB
JavaScript
/**
|
|
* @format
|
|
*/
|
|
|
|
import { AppRegistry, Platform } from 'react-native';
|
|
import App from './App';
|
|
import { name as appName } from './app.json';
|
|
import database from '@react-native-firebase/database';
|
|
import { LogBox } from 'react-native';
|
|
import ReactNativeForegroundService from '@supersami/rn-foreground-service';
|
|
import notifee from '@notifee/react-native';
|
|
import { handleNotificationView, handlePushNotification } from '@hooks/useFCM/useFCM';
|
|
import messaging from '@react-native-firebase/messaging';
|
|
import CosmosForegroundService, {
|
|
FOREGROUND_SERVICE_CONFIG,
|
|
} from '@services/foregroundServices/foreground.service';
|
|
import axiosInstance, { ApiKeys, getApiUrl } from '@components/utlis/apiHelper';
|
|
import { StorageKeys } from '@interfaces/storageKeys';
|
|
import { AgentActivity } from '@interfaces/agentActivity';
|
|
import { FOREGROUND_TASKS } from '@common/TrackingComponent';
|
|
import { MILLISECONDS_IN_A_MINUTE } from '@rn-ui-lib/utils/common';
|
|
import Geolocation from 'react-native-geolocation-service';
|
|
import ForegroundService from '@supersami/rn-foreground-service';
|
|
import { getItem } from '@components/utlis/storageHelper';
|
|
import { AppState } from 'react-native';
|
|
|
|
if (__DEV__) {
|
|
LogBox.ignoreAllLogs();
|
|
}
|
|
|
|
const geolocationForegroundTask = async () => {
|
|
Geolocation.getCurrentPosition(async (position) => {
|
|
const { latitude, longitude, accuracy } = position.coords || {};
|
|
if (!latitude || !longitude || !accuracy) return;
|
|
|
|
const isActiveOnApp = (await getItem(StorageKeys.IS_USER_ACTIVE)) || false;
|
|
const userActivityonApp =
|
|
(await getItem(StorageKeys.USER_ACTIVITY_ON_APP)) || AgentActivity.LOW;
|
|
|
|
const geolocation = [
|
|
{
|
|
latitude,
|
|
longitude,
|
|
accuracy,
|
|
timestamp: Date.now(),
|
|
isActiveOnApp: Boolean(isActiveOnApp),
|
|
userActivityOnApp: String(userActivityonApp),
|
|
},
|
|
];
|
|
axiosInstance.post(getApiUrl(ApiKeys.SEND_LOCATION), geolocation, {
|
|
headers: {
|
|
donotHandleError: 'true',
|
|
},
|
|
});
|
|
});
|
|
};
|
|
|
|
// Background push notification handler
|
|
messaging().setBackgroundMessageHandler(async (remoteMessage) => {
|
|
const { data } = remoteMessage || {};
|
|
if (data?.payload) {
|
|
const notification = JSON.parse(data.payload);
|
|
if (notification?.showAsPn) {
|
|
handlePushNotification(notification);
|
|
}
|
|
// TODO: Try to start foreground service if not working.
|
|
// const isForegroundServiceRunning = await ForegroundService.is_running();
|
|
// console.log('isForegroundServiceRunning::', isForegroundServiceRunning);
|
|
// if (!isForegroundServiceRunning) {
|
|
// ForegroundService.add_task(geolocationForegroundTask, {
|
|
// delay: 6000,
|
|
// onLoop: true,
|
|
// taskId: FOREGROUND_TASKS.GEOLOCATION,
|
|
// });
|
|
// ForegroundService.start(FOREGROUND_SERVICE_CONFIG);
|
|
// }
|
|
}
|
|
});
|
|
notifee.onBackgroundEvent(async (event) => {
|
|
handleNotificationView(event, true);
|
|
});
|
|
|
|
database().setPersistenceEnabled(true);
|
|
ReactNativeForegroundService.register();
|
|
|
|
// Register the headless task
|
|
|
|
AppRegistry.registerComponent(appName, () => App);
|