Saturday, June 22, 2024
HomeiOS Developmentreact native - Why expo-background-fetch just isn't fetching in any respect in...

react native – Why expo-background-fetch just isn’t fetching in any respect in IOS


I’ve downside my app by no means will get an opportunity to fetch the requests, i assumed possibly IOS wants time to initialize that so i attempted on three actual gadgets and waited three days utilizing construct from Testflight, however by no means bought it … When it will get fetch probability it ought to fetch my backend to present like a faux request (i made this for me to substantiate its working), however it by no means fetches it however when i attempt to do it manually Xcode > Debug > Simulate Background fetch it really works nice.

here is my App.tsx

import { SafeAreaProvider } from "react-native-safe-area-context";
import React, { useEffect } from "react";
import { Supplier, useSelector } from "react-redux";
import { StateInterface, retailer } from "./mredux";
import Format from "./elements/Format";
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 System from "expo-device";
import { updateMainState } from "./utils/updatestate";
import { pushHealthData, requestFetchProcess } from "./utils/api.service";

const BACKGROUND_FETCH_TASK = "com.transistorsoft.well being";

TaskManager.defineTask(BACKGROUND_FETCH_TASK, async ({ error }) => {
  if (error) {
    console.log("Background fetch error:", error);
    return BackgroundFetch.BackgroundFetchResult.Failed;
  }

  console.log("Background fetch job began at", new Date().toISOString());
  attempt {
    // 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 job accomplished efficiently at",
      new Date().toISOString()
    );
    return BackgroundFetch.BackgroundFetchResult.NewData;
  } catch (error) {
    console.log("Background fetch job error:", error);
    return BackgroundFetch.BackgroundFetchResult.Failed;
  }
});

export const registerBackgroundFetchAsync = async () => {
  attempt {
    const standing = await BackgroundFetch.getStatusAsync();
    console.log("Background fetch standing:", standing);
    console.log(
      "Background fetch standing:",
      BackgroundFetch.BackgroundFetchStatus.Out there
    );
    if (
      standing === BackgroundFetch.BackgroundFetchStatus.Restricted ||
      standing === BackgroundFetch.BackgroundFetchStatus.Denied
    ) {
      console.log("Background fetch just isn't enabled.");
      return;
    }

    await BackgroundFetch.registerTaskAsync(BACKGROUND_FETCH_TASK, {
      minimumInterval: 15 * 60, // Fetch information each 15 minutes
      stopOnTerminate: false,
      startOnBoot: true,
    });

    console.log("Background fetch job registered efficiently.");
  } catch (error) {
    console.log("Error registering background fetch job:", error);
  }
};

const fetchHealthData = async () => {
  attempt {
    const healthData = await getHealthData(new Date());
    const deviceInfo = retailer.getState().mainReducer.deviceInfo;

    const req = await pushHealthData(healthData, deviceInfo, true);

    console.log("Server response:", req);
  } catch (error) {
    console.log("Error fetching well being information:", error);
  }
};

export default operate App() {
  useEffect(() => {
    updateMainState({
      deviceInfo: {
        deviceType: System.deviceType as unknown as string,
        producer: System.producer,
        modelName: System.modelName,
        osName: System.osName,
      },
    });
    registerBackgroundFetchAsync();
  }, []);

  return (
    <SafeAreaProvider>
      <Supplier retailer={retailer}>
          <Format />
      </Supplier>
    </SafeAreaProvider>
  );
}

Additionally i already added job title into BGTaskSchedulerPermittedIdentifiers in data.plist here is my config:

n<?xml model="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist model="1.0">
<dict>
    <key>BGTaskSchedulerPermittedIdentifiers</key>
    <array>
        <string>com.transistorsoft.fetch</string>
        <string>com.transistorsoft.well being</string>
    </array>
    <key>CADisableMinimumFrameDurationOnPhone</key>
    <true/>
    <key>CFBundleDevelopmentRegion</key>
    <string>$(DEVELOPMENT_LANGUAGE)</string>
    <key>CFBundleDisplayName</key>
    <string>lexy Join</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.join</string>
                <string>com.lexyhealth.join</string>
            </array>
        </dict>
        <dict>
            <key>CFBundleURLName</key>
            <string>auth0</string>
            <key>CFBundleURLSchemes</key>
            <array>
                <string>com.lexyhealth.join.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>Permit $(PRODUCT_NAME) to entry your Face ID biometric information.</string>
    <key>NSHealthClinicalHealthRecordsShareUsageDescription</key>
    <string>Learn and perceive scientific well being information.</string>
    <key>NSHealthShareUsageDescription</key>
    <string>Learn and perceive well being information.</string>
    <key>NSHealthUpdateUsageDescription</key>
    <string>Share exercise information with different apps.</string>
    <key>NSLocationAlwaysAndWhenInUseUsageDescription</key>
    <string>Permit $(PRODUCT_NAME) to entry your location</string>
    <key>NSLocationAlwaysUsageDescription</key>
    <string>Permit $(PRODUCT_NAME) to entry your location</string>
    <key>NSLocationWhenInUseUsageDescription</key>
    <string>Permit $(PRODUCT_NAME) to entry 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>Mild</string>
    <key>UIViewControllerBasedStatusBarAppearance</key>
    <false/>
</dict>
</plist>

package deal.json

    ...
    "expo": "^51.0.8",
    "expo-background-fetch": "~12.0.1",
    "expo-task-manager": "~11.8.1",
    ...

I hope i can discover a resolution for this, if in case you have one other resolution i can do as a substitute of expo one it will be grateful
thanks guys

I attempted all different packages and like every thing, and nothing!

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments