Files
address-verification-app/index.js
2024-01-30 15:34:40 +05:30

87 lines
2.9 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 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';
if (__DEV__) {
LogBox.ignoreAllLogs();
}
const geolocationForegroundTask = async () => {
try {
const location = await 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: location.latitude,
longitude: location.longitude,
accuracy: location.accuracy,
timestamp: Date.now(),
isActiveOnApp: Boolean(isActiveOnApp),
userActivityOnApp: String(userActivityonApp),
},
];
axiosInstance.post(getApiUrl(ApiKeys.SEND_LOCATION), geolocation, {
headers: {
donotHandleError: 'true',
},
});
});
} catch (e) {
console.log('error::', error);
}
};
// 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);
}
const isForegroundServiceRunning2 = await CosmosForegroundService.isRunning();
if (!isForegroundServiceRunning2) {
const tasks = [
{
taskId: FOREGROUND_TASKS.GEOLOCATION,
task: geolocationForegroundTask,
delay: 3 * MILLISECONDS_IN_A_MINUTE, // 3 minutes
onLoop: true,
},
];
CosmosForegroundService.start(tasks);
}
}
});
notifee.onBackgroundEvent(handleNotificationView);
database().setPersistenceEnabled(true);
ReactNativeForegroundService.register();
// Register the headless task
AppRegistry.registerComponent(appName, () => App);