question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

AppState incorrectly says `active` when launched from a geofence in the background

See original GitHub issue

Please provide all the information requested. Issues that do not follow this format are likely to stall.

Description

My app will be launched in the background response to a geo-fence event. In the task handler for being launched, I log the app state of the app. What I’ve noticed is that the app state is active however the app is 100% running in the background.

I have provided some code to get you an insight, but in reality testing this with Geofencing takes some knowledge of geofencing. I’d be happy to share more code if we determine that this bug can be debugged 😃

React Native version:

    OS: macOS 10.15.2
    CPU: (16) x64 Intel(R) Core(TM) i9-9880H CPU @ 2.30GHz
    Memory: 21.33 MB / 16.00 GB
    Shell: 3.2.57 - /bin/bash
  Binaries:
    Node: 12.13.0 - ~/.nvm/versions/node/v12.13.0/bin/node
    Yarn: 1.19.1 - ~/.nvm/versions/node/v12.13.0/bin/yarn
    npm: 6.14.4 - ~/.nvm/versions/node/v12.13.0/bin/npm
    Watchman: 4.9.0 - /usr/local/bin/watchman
  SDKs:
    iOS SDK:
      Platforms: iOS 13.2, DriverKit 19.0, macOS 10.15, tvOS 13.2, watchOS 6.1
  IDEs:
    Xcode: 11.3/11C29 - /usr/bin/xcodebuild
  npmPackages:
    react: 16.9.0 => 16.9.0
    react-native: 0.61.5 => 0.61.5

Steps To Reproduce

EDIT: This issue was reproduced in a sample app which is posted in a comment below. See https://github.com/facebook/react-native/issues/28655#issuecomment-629245257 for Sample App that demonstrates the bug clearly. You should use that code and ignore my steps below (which are more complicated since they also use the geofencing API)

Provide a detailed list of steps that reproduce the issue.

  1. Setup a geofence to wake up your app in the background
  2. Kill the app and go to the home screen.
  3. Trigger geofence (eg using simulator location).
  4. App launches in the background and you’ll see logs. Log the value of AppState and you will see it to be active

Expected Results

AppState should be backgrounded

Snack, code example, screenshot, or link to a repository:

TaskManager.defineTask(GEOFENCING_TASK, ({ data: { eventType, region }, error }) => {
    if (error) {
      // check `error.message` for more details.
      console.log(`TaskManager Error: ${error.message}`);
      return;
    }
    console.log(`App State is ${AppState.currentState}`)
  
});

Example to setup geofencing:

// add one more region!
  regions.push({
    identifier: `foo`,
    latitude: location.coords.latitude,
    longitude: location.coords.longitude,
    radius: GEOFENCE_RADIUS,
    notifyOnEnter: true,
    notifyOnExit: true,
  });

  try {
    console.log(`[LOCATION MANAGER] Setting up geofencing. Count= ${regions.length}`);
    let result = await Location.startGeofencingAsync(GEOFENCING_TASK, regions);

Issue Analytics

  • State:open
  • Created 3 years ago
  • Comments:8 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
LaurenceRolandJamescommented, May 15, 2020

This issue isn’t related to geofences. It happens in a newly created React Native app.

@safaiyeh

I’ve reproduced the issue in a new React Native app here: https://github.com/LaurenceRolandJames/AppStateActiveRepro/tree/master

2reactions
cHaLkdusTcommented, Apr 19, 2020

I’ve tested the class-based component version of the code snippet, it is working as expected. There seems an issue when using the functional component that uses useState hook.

Read more comments on GitHub >

github_iconTop Results From Across the Web

[SOLVED] Problem getting geofences to work in later versions ...
Start in geoence: I started the app with me and the devices in the geofence zone, put the app in the background and...
Read more >
WKWatchConnectivityRefreshBa...
When I activate the home button on the watch, the watch app goes to the background, handle(_ backgroundTasks: Set<WKRefreshBackgroundTask>) is ...
Read more >
Geofencing problems with iOS15.01 - Apple Community
Although Location is Always On, some geofenced apps don't update the location at all, some only when app is opened. (for instance Honeywell...
Read more >
Geofencing API causes Excessive Wi-Fi Scanning [78591747]
The attached bugreport shows 5 background WiFi scans attributed to this app over a 32.6 ... We recently started facing the same problem...
Read more >
Geographical restrictions for application usage on a mobile ...
However, with this growth another problem is arising, namely, the security and management of ... then the GeoFence object will allow the app...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found