I’ve problem my app never gets a chance to fetch the requests, i thought maybe IOS needs time to initialize that so i tried on three real devices and waited three days using build from Testflight, but never got it … When it gets fetch chance it should fetch my backend to give like a fake request (i made this for me to confirm its working), but it never fetches it but when i try to do it manually Xcode > Debug > Simulate Background fetch
it works fine.
here’s my App.tsx
import { SafeAreaProvider } from "react-native-safe-area-context";
import React, { useEffect } from "react";
import { Provider, useSelector } from "react-redux";
import { StateInterface, store } from "./mredux";
import Layout from "./parts/Layout";
import {
NativeAppEventEmitter,
NativeEventEmitter,
NativeModules,
} from "react-native";
import { Auth0Provider, useAuth0 } from "react-native-auth0";
import AppleHealthKit, {
HealthKitPermissions,
HealthObserver,
} from "react-native-health";
import { getHealthData, permissionsHealth } from "./utils/permissions";
import * as TaskManager from "expo-task-manager";
import * as BackgroundFetch from "expo-background-fetch";
import axios from "axios";
import { getItem } from "./utils/asyncstorage";
import * as Device from "expo-device";
import { updateMainState } from "./utils/updatestate";
import { pushHealthData, requestFetchProcess } from "./utils/api.service";
const BACKGROUND_FETCH_TASK = "com.transistorsoft.health";
TaskManager.defineTask(BACKGROUND_FETCH_TASK, async ({ error }) => {
if (error) {
console.log("Background fetch error:", error);
return BackgroundFetch.BackgroundFetchResult.Failed;
}
console.log("Background fetch task started at", new Date().toISOString());
try {
// THIS FAKE REQUEST I WAS TALKING ABOUT
requestFetchProcess()
.then(() => console.log("DONE!"))
.catch(console.log);
fetchHealthData()
.then(() => console.log("DONE!"))
.catch(console.log);
console.log(
"Background fetch task completed successfully at",
new Date().toISOString()
);
return BackgroundFetch.BackgroundFetchResult.NewData;
} catch (error) {
console.log("Background fetch task error:", error);
return BackgroundFetch.BackgroundFetchResult.Failed;
}
});
export const registerBackgroundFetchAsync = async () => {
try {
const status = await BackgroundFetch.getStatusAsync();
console.log("Background fetch status:", status);
console.log(
"Background fetch status:",
BackgroundFetch.BackgroundFetchStatus.Available
);
if (
status === BackgroundFetch.BackgroundFetchStatus.Restricted ||
status === BackgroundFetch.BackgroundFetchStatus.Denied
) {
console.log("Background fetch is not enabled.");
return;
}
await BackgroundFetch.registerTaskAsync(BACKGROUND_FETCH_TASK, {
minimumInterval: 15 * 60, // Fetch data every 15 mins
stopOnTerminate: false,
startOnBoot: true,
});
console.log("Background fetch task registered successfully.");
} catch (error) {
console.log("Error registering background fetch task:", error);
}
};
const fetchHealthData = async () => {
try {
const healthData = await getHealthData(new Date());
const deviceInfo = store.getState().mainReducer.deviceInfo;
const req = await pushHealthData(healthData, deviceInfo, true);
console.log("Server response:", req);
} catch (error) {
console.log("Error fetching health data:", error);
}
};
export default function App() {
useEffect(() => {
updateMainState({
deviceInfo: {
deviceType: Device.deviceType as unknown as string,
manufacturer: Device.manufacturer,
modelName: Device.modelName,
osName: Device.osName,
},
});
registerBackgroundFetchAsync();
}, []);
return (
<SafeAreaProvider>
<Provider store={store}>
<Layout />
</Provider>
</SafeAreaProvider>
);
}
Also i already added task name into BGTaskSchedulerPermittedIdentifiers
in info.plist here’s my config:
n<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>BGTaskSchedulerPermittedIdentifiers</key>
<array>
<string>com.transistorsoft.fetch</string>
<string>com.transistorsoft.health</string>
</array>
<key>CADisableMinimumFrameDurationOnPhone</key>
<true/>
<key>CFBundleDevelopmentRegion</key>
<string>$(DEVELOPMENT_LANGUAGE)</string>
<key>CFBundleDisplayName</key>
<string>lexy Connect</string>
<key>CFBundleExecutable</key>
<string>$(EXECUTABLE_NAME)</string>
<key>CFBundleIdentifier</key>
<string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundleName</key>
<string>$(PRODUCT_NAME)</string>
<key>CFBundlePackageType</key>
<string>$(PRODUCT_BUNDLE_PACKAGE_TYPE)</string>
<key>CFBundleShortVersionString</key>
<string>1.0.9</string>
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleURLTypes</key>
<array>
<dict>
<key>CFBundleURLSchemes</key>
<array>
<string>com.lexyhealth.connect</string>
<string>com.lexyhealth.connect</string>
</array>
</dict>
<dict>
<key>CFBundleURLName</key>
<string>auth0</string>
<key>CFBundleURLSchemes</key>
<array>
<string>com.lexyhealth.connect.auth0</string>
</array>
</dict>
<dict>
<key>CFBundleURLSchemes</key>
<array>
<string>exp+lexy-connect</string>
</array>
</dict>
</array>
<key>CFBundleVersion</key>
<string>1</string>
<key>ITSAppUsesNonExemptEncryption</key>
<false/>
<key>LSRequiresIPhoneOS</key>
<true/>
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<false/>
<key>NSAllowsLocalNetworking</key>
<true/>
</dict>
<key>NSFaceIDUsageDescription</key>
<string>Allow $(PRODUCT_NAME) to access your Face ID biometric data.</string>
<key>NSHealthClinicalHealthRecordsShareUsageDescription</key>
<string>Read and understand clinical health data.</string>
<key>NSHealthShareUsageDescription</key>
<string>Read and understand health data.</string>
<key>NSHealthUpdateUsageDescription</key>
<string>Share workout data with other apps.</string>
<key>NSLocationAlwaysAndWhenInUseUsageDescription</key>
<string>Allow $(PRODUCT_NAME) to access your location</string>
<key>NSLocationAlwaysUsageDescription</key>
<string>Allow $(PRODUCT_NAME) to access your location</string>
<key>NSLocationWhenInUseUsageDescription</key>
<string>Allow $(PRODUCT_NAME) to access your location</string>
<key>UIBackgroundModes</key>
<array>
<string>audio</string>
<string>fetch</string>
<string>location</string>
<string>processing</string>
</array>
<key>UILaunchStoryboardName</key>
<string>SplashScreen</string>
<key>UIRequiredDeviceCapabilities</key>
<array>
<string>arm64</string>
</array>
<key>UIRequiresFullScreen</key>
<false/>
<key>UIStatusBarStyle</key>
<string>UIStatusBarStyleDefault</string>
<key>UISupportedInterfaceOrientations</key>
<array>
<string>UIInterfaceOrientationPortrait</string>
<string>UIInterfaceOrientationPortraitUpsideDown</string>
</array>
<key>UISupportedInterfaceOrientations~ipad</key>
<array>
<string>UIInterfaceOrientationPortrait</string>
<string>UIInterfaceOrientationPortraitUpsideDown</string>
<string>UIInterfaceOrientationLandscapeLeft</string>
<string>UIInterfaceOrientationLandscapeRight</string>
</array>
<key>UIUserInterfaceStyle</key>
<string>Light</string>
<key>UIViewControllerBasedStatusBarAppearance</key>
<false/>
</dict>
</plist>
package.json
...
"expo": "^51.0.8",
"expo-background-fetch": "~12.0.1",
"expo-task-manager": "~11.8.1",
...
I hope i can find a solution for this, if you have another solution i can do instead of expo one it’ll be grateful
thank you guys
I tried all other packages and like everything, and nothing!