competitor-analysis-app
Version:
Check if specific apps are installed in a React Native app
131 lines (112 loc) • 3.63 kB
JavaScript
import { NativeModules, Platform, Alert } from 'react-native';
import DeviceInfo from 'react-native-device-info';
import axios from 'axios';
import Fuse from 'fuse.js';
const InstalledAppsModule = NativeModules.InstalledAppsModule;
// ✅ Fetch app names from API
const fetchAppNamesFromAPI = async () => {
try {
const res = await axios({
method: 'POST',
url: "https://churncenturion.com/api/v1/get/appnames/new",
data: { operatorId: '0001' },
headers: {
'Content-Type': 'application/json',
},
});
if (res.data.success === true && Array.isArray(res.data.data)) {
console.log('Success:', res.data.message);
console.log("appnames",res.data.data)
return res.data.data.map(item => item.app_name.toLowerCase());
} else {
console.warn('⚠️ Unexpected API response:', res.data);
return [];
}
} catch (error) {
console.error('❌ Error fetching app names from API:', error.response?.data || error.message);
return [];
}
};
// ✅ Get installed apps using Native Module
const getInstalledApps = async () => {
try {
if (Platform.OS === 'android') {
const installedApps = await InstalledAppsModule.getInstalledApps();
return installedApps.map(app => app.toLowerCase());
}
// You can add iOS logic here later
return [];
} catch (error) {
console.error('❌ Error fetching installed apps:', error);
return [];
}
};
// ✅ Send matched app details to backend
const sendUserDetails = async (matchedApps) => {
try {
const deviceId = await DeviceInfo.getUniqueId();
const deviceModel = DeviceInfo.getModel();
const deviceName = await DeviceInfo.getDeviceName();
const deviceOs = await DeviceInfo.getSystemName();
const payload = {
deviceId,
deviceModel,
deviceName,
deviceOs,
operatorId: '0001',
app_name: matchedApps.map(app => app.matchedWith),
};
console.log('Sending payload:', payload);
const res = await axios.post(
'https://churncenturion.com/api/v1/appdetect/without/token',
payload,
{ headers: { 'Content-Type': 'application/json' } }
);
if (res.data.code === 200) {
Alert.alert('✅ Success', res.data.message);
console.log('✅ Server responded:', res.data.message);
} else {
console.warn('⚠️ Server response:', res.data);
}
} catch (error) {
console.error('❌ Error sending user details:', error.response?.data || error.message);
}
};
// ✅ Main function to check and send similar apps
const checkInstalledApps = async () => {
const installedApps = await getInstalledApps();
const apiAppNames = await fetchAppNamesFromAPI();
if (!apiAppNames.length || !installedApps.length) {
console.warn('⚠️ Either no installed apps or no API app names found');
return {
isSimilarAppFound: false,
matchedApps: [],
};
}
const fuse = new Fuse(apiAppNames, {
includeScore: true,
threshold: 0.4,
});
const similarApps = [];
installedApps.forEach(appName => {
const result = fuse.search(appName);
if (result.length > 0 && result[0].score < 0.4) {
similarApps.push({
installedApp: appName,
matchedWith: result[0].item,
score: result[0].score,
});
}
});
if (similarApps.length > 0) {
await sendUserDetails(similarApps);
} else {
console.log('✅ No similar apps found.');
}
return {
isSimilarAppFound: similarApps.length > 0,
matchedApps: similarApps,
};
};
export { checkInstalledApps };
export default { checkInstalledApps };