transistorsoft / react-native-background-fetch

Periodic callbacks in the background for both IOS and Android
MIT License
1.5k stars 193 forks source link

Unable to simulate background events #538

Open daamsie opened 2 weeks ago

daamsie commented 2 weeks ago

Your Environment

Expected Behavior

I expect to see an event logged when I run the simulated task in XCode as described in the ReadMe

Actual Behavior

Nothing happens

Steps to Reproduce

I have the following function that initialises the background fetch. Very similar to the sample code.

const initBackgroundFetch = async () => {
    console.log('Initialising background fetch now');
    try {
      const status = await BackgroundFetch.configure(
        {
          minimumFetchInterval: 15,
          requiredNetworkType: BackgroundFetch.NETWORK_TYPE_NONE, // Run regardless of network type
        },
        async (taskId: string) => {
          console.log('[BackgroundFetch] task: ', taskId);
          await handleSyncEvent();
          BackgroundFetch.finish(taskId);
        },
        async (taskId: string) => {
          console.error('[BackgroundFetch] TIMEOUT task: ', taskId);
          BackgroundFetch.finish(taskId);
        },
      );

      if (status === BackgroundFetch.STATUS_RESTRICTED) {
        console.error('[BackgroundFetch] configure failed: STATUS_RESTRICTED');
      } else if (status === BackgroundFetch.STATUS_DENIED) {
        console.error('[BackgroundFetch] configure failed: STATUS_DENIED');
      } else if (status === BackgroundFetch.STATUS_AVAILABLE) {
        console.log('[BackgroundFetch] configure status is STATUS_AVAILABLE');
      }
    } catch (e) {
      console.error('[BackgroundFetch] configure failed: ', e);
    }
  };

When running app, it successfully logs the following lines:

[TSBackgroundFetch scheduleBGAppRefresh] com.transistorsoft.fetch [BackgroundFetch] configure status is STATUS_AVAILABLE

I pause execution in xcode, then paste in the command to trigger an event e -l objc -- (void)[[BGTaskScheduler sharedScheduler] _simulateLaunchForTaskWithIdentifier:@"com.transistorsoft.fetch"]

I restart execution.

Nothing happens.

What I'm expecting is this to log something

console.log('[BackgroundFetch] task: ', taskId);

Context

I have tried installing the example app as a sanity check and got simulated events firing just fine there.

Really stumped and hoping there is something else I can try to get this going. Am I misunderstanding what should be happening here?

Debug logs

christocracy commented 2 weeks ago

Post your Info.plist

daamsie commented 2 weeks ago

Here we go.. interestingly, it does actually seem to be working in real use - after a day or so it starts doing the background fetches. I'd still like to be able to perform the simulated events though for my own sanity.

<?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.losttravel.tracker</string>
        <string>com.transistorsoft.fetch</string>
    </array>
    <key>CFBundleDevelopmentRegion</key>
    <string>en</string>
    <key>CFBundleDisplayName</key>
    <string>Wayward</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>APPL</string>
    <key>CFBundleShortVersionString</key>
    <string>$(MARKETING_VERSION)</string>
    <key>CFBundleSignature</key>
    <string>????</string>
    <key>CFBundleURLTypes</key>
    <array>
        <dict>
            <key>CFBundleTypeRole</key>
            <string>Editor</string>
            <key>CFBundleURLName</key>
            <string>io.Branch.Wayward</string>
            <key>CFBundleURLSchemes</key>
            <array>
                <string>com.losttravel.tracker</string>
            </array>
        </dict>
    </array>
    <key>CFBundleVersion</key>
    <string>684</string>
    <key>ITSAppUsesNonExemptEncryption</key>
    <false/>
    <key>LSApplicationCategoryType</key>
    <string/>
    <key>LSRequiresIPhoneOS</key>
    <true/>
    <key>NSAppTransportSecurity</key>
    <dict>
        <key>NSAllowsArbitraryLoads</key>
        <false/>
        <key>NSAllowsLocalNetworking</key>
        <true/>
    </dict>
    <key>NSCameraUsageDescription</key>
    <string>Wayward needs access to your Camera</string>
    <key>NSLocationAlwaysAndWhenInUseUsageDescription</key>
    <string>In order to reliably track your adventures, Wayward requires location settings to be set to “Always”.</string>
    <key>NSLocationWhenInUseUsageDescription</key>
    <string>To track your adventures</string>
    <key>NSPhotoLibraryAddUsageDescription</key>
    <string>Select photos from your library</string>
    <key>NSPhotoLibraryUsageDescription</key>
    <string>Select photos from your library</string>
    <key>UIBackgroundModes</key>
    <array>
        <string>fetch</string>
        <string>location</string>
    </array>
    <key>UILaunchStoryboardName</key>
    <string>LaunchScreen</string>
    <key>UIRequiredDeviceCapabilities</key>
    <array>
        <string>arm64</string>
    </array>
    <key>UISupportedInterfaceOrientations</key>
    <array>
        <string>UIInterfaceOrientationLandscapeLeft</string>
        <string>UIInterfaceOrientationLandscapeRight</string>
        <string>UIInterfaceOrientationPortrait</string>
    </array>
    <key>UIViewControllerBasedStatusBarAppearance</key>
    <false/>
</dict>
</plist>
christocracy commented 2 weeks ago
<key>BGTaskSchedulerPermittedIdentifiers</key>
    <array>
        <string>com.losttravel.tracker</string>
        <string>com.transistorsoft.fetch</string>
    </array>

What is this identifier for <string>com.losttravel.tracker</string>?

daamsie commented 1 week ago

This is the identifier of the app itself. We also do background location tracking and I believe that is why that was added in there. Though looking at it now, I'm not entirely certain it's needed.

christocracy commented 1 week ago

We also do background location tracking

what has it to do with background location tracking? I’ve been managing a background location tracking plug-in for over 10 years and BGTaskSchedulerPermittedIdentifiers has nothing to do with background location tracking.

daamsie commented 1 week ago

Like I said, I'm not certain it's needed. We use Radar for iOS location tracking. They also do some syncing (which we're not using), so maybe that's how it got in there? Or maybe (probably more likely) it's just something dumb I added in at some point to try and get things working.

If having multiple identifiers in that array makes it impossible to fire the simulated event as above, I can try removing it.

christocracy commented 1 week ago

How much do you pay for Radar? They have a whole page comparing themselves to my plug-in.

daamsie commented 1 week ago

Nothing. We don't use any of their paid features.

christocracy commented 1 week ago

Tim Hortons, a Canadian cafe chain, was found to have violated privacy laws with their app using Radar.

christocracy commented 1 week ago

a contract with a U.S.-based third-party location services supplier contained language “so vague and permissive” that it left the door open for this company to sell the location data for its own purposes..

Though not named in the investigation, the app was developed with assistance from Radar Labs. Tim Hortons removed Radar Labs’ technology from the app in 2020.

IMG_2451

daamsie commented 1 week ago

We're not syncing the tracking data to Radar, which is why it doesn't cost anything. They don't have any user data to share with third parties.

I tried removing the seemingly redundant identifier. Simulating events still does nothing.

christocracy commented 1 week ago

I suggest you create for yourself a simple HelloWorld app and test in isolation

daamsie commented 1 week ago

Ok. Thanks for reviewing the issue. Knowing that it works now if just let run for a day, I'm not sure how much more debugging I can justify doing to find out why the simulated event doesn't work, but I'll keep that in mind as the next thing to try.