This article is about fixing Background service does not work in SDK 31, app crashes in mauron85 react-native-background-geolocation
  • 07-Feb-2023
Lightrun Team
Author Lightrun Team
Share
This article is about fixing Background service does not work in SDK 31, app crashes in mauron85 react-native-background-geolocation

Background service does not work in SDK 31, app crashes in mauron85 react-native-background-geolocation

Lightrun Team
Lightrun Team
07-Feb-2023

Explanation of the problem

Environment:

  • Plugin version: 0.6.3
  • Platform: Android
  • OS version: 12
  • Device: Simulator, Samsung
  • Running in Simulator: Yes
  • React Native version: 0.66.4

Problem: After upgrading to SDK 31, the background location service in an Android app no longer works and the app crashes in the background. The service was working perfectly in Android 9, 10, and 11.

Expected Behavior: The background service should start and run successfully in an app with targetSdkVersion = 31.

Actual Behavior: The background service does not start and the app crashes in the background.

Possible Fix: Modify line 77 in https://github.com/mauron85/background-geolocation-android/blob/58464febf6901f1e489817e4425418d48443929b/src/main/java/com/marianhello/bgloc/sync/NotificationHelper.java by adding FLAG_IMMUTABLE.

Steps to Reproduce:

  1. App switches to the background
  2. BackgroundGeolocation.on(‘background’) event
  3. Call BackgroundGeolocation.start()
  4. No BackgroundGeolocation.on(‘start’) event
  5. Notification does NOT appear
  6. One BackgroundGeolocation.on(‘location’) event
  7. App crashes.

Debug Logs: The following logs were recorded at 12-31 10:37:15.886:

  • User granted requested permissions
  • Attempt to start bg service
  • Creating LocationServiceImpl
  • Account {name=xxxxxxxxx, type=com.xxxx.mauron85.bgloc.account} already exists, skipping
  • Creating PostLocationTask
  • Service in [NOT STARTED] state. cmdId: [1]. startId: [1]
  • Network condition changed has connectivity: true
  • Attempt to start unconfigured service. Will use stored or default.
  • Will start service with specified configuration.
  • Creating RawLocationProvider

Troubleshooting with the Lightrun Developer Observability Platform

Getting a sense of what’s actually happening inside a live application is a frustrating experience, one that relies mostly on querying and observing whatever logs were written during development.
Lightrun is a Developer Observability Platform, allowing developers to add telemetry to live applications in real-time, on-demand, and right from the IDE.

  • Instantly add logs to, set metrics in, and take snapshots of live applications
  • Insights delivered straight to your IDE or CLI
  • Works where you do: dev, QA, staging, CI/CD, and production

Start for free today

Problem solution for Background service does not work in SDK 31, app crashes in mauron85 react-native-background-geolocation

The problem described in the answer involves using the react-native-track-player package in a React Native project. The package provides a simple way to play audio tracks in the background on both Android and iOS devices. However, it can sometimes face issues when trying to run the code in a project.

The solution provided in the answer involves updating the line in the package.json file that references the react-native-track-player package. Instead of using the default version of the package, the line should be updated to a specific Git commit hash (#bdd731f6dd76e14431d3a97b2950ec0f9ac8dc1d) to reference a specific version of the package. This is done because that particular version of the package is known to work well and avoid issues that might arise from using a different version.

After updating the line in the package.json file, the next step is to clean and rebuild the project. This ensures that all the dependencies are updated, and the project is using the correct version of the react-native-track-player package. According to the answer, after following these steps, the project should work correctly and play audio tracks in the background as intended. The proposed solution has been tested and confirmed to work, so it is a reliable solution to resolve the issue with the react-native-track-player package in a React Native project.

"react-native-track-player": "https://github.com/react-native-kit/react-native-track-player.git#bdd731f6dd76e14431d3a97b2950ec0f9ac8dc1d",

Other popular problems with react-native-background-geolocation

Problem: Background Task Not Running

One of the problems with react-native-background-geolocation is that the background task doesn’t seem to be running, even though the app is properly configured. This issue often arises because of the limitations placed on background tasks by the operating system.

Solution:

In order to resolve this issue, developers must ensure that the background task is set up properly, including setting the desired accuracy and minimum distance filter.

backgroundGeolocation.configure({
    desiredAccuracy: backgroundGeolocation.DESIRED_ACCURACY_HIGH,
    distanceFilter: 10,
    stopOnTerminate: false,
    startOnBoot: true,
    debug: false,
    locationProvider: backgroundGeolocation.ACTIVITY_PROVIDER
});

Problem: Location Updates Not Triggered

Another common issue with react-native-background-geolocation is that location updates are not being triggered, even when the app is properly configured. This issue can occur due to a variety of reasons, including incorrect GPS data, low battery levels, and lack of network coverage.

Solution:

To resolve this issue, developers should make sure that the device has an adequate GPS signal, is running on enough battery power, and has a good network connection.

navigator.geolocation.getCurrentPosition(
  (position) => {
    console.log("Current position: ", position);
  },
  (error) => {
    console.log("Error getting current position: ", error);
  },
  {enableHighAccuracy: true, timeout: 20000, maximumAge: 1000}
);

Problem: Inconsistent Location Data

A common issue with react-native-background-geolocation is inconsistent location data, where the app is not able to accurately track the device’s location. This issue can arise due to a number of factors, including poor GPS signal, interference from other apps, and incorrect settings.

Solution:

To resolve this issue, developers should ensure that the device’s GPS is functioning properly, and that the app is configured correctly, with the appropriate accuracy and distance filter settings.

backgroundGeolocation.getCurrentLocation((location) => {
  console.log("Current location: ", location);
}, (error) => {
  console.log("Error getting current location: ", error);
});

A brief introduction to react-native-background-geolocation

React-Native-Background-Geolocation is a highly-efficient and versatile library for tracking the location of an individual in the background of a React Native app. This library makes use of device-level APIs such as GPS, Wi-Fi, and cellular network to provide accurate location data even when the app is running in the background. The library is designed to support both Android and iOS platforms, offering cross-platform compatibility for React Native developers.

One of the key features of React-Native-Background-Geolocation is its ability to track location changes in real-time. This enables developers to build location-based applications that can provide relevant information to users, even when the app is running in the background. The library provides a set of APIs that can be used to customize the location tracking process, such as adjusting the frequency of location updates, setting geofences, and more. Additionally, the library includes features such as power-saving mode, location persistence, and device activity recognition, making it a comprehensive solution for tracking location in React Native applications.

Most popular use cases for react-native-background-geolocation

  1. Location tracking: React Native Background Geolocation can be utilized to track the location of a user in real-time, even when the app is running in the background. This can be useful for a variety of use cases, such as logistics, delivery, or tracking the movement of a user for safety reasons. The following code block demonstrates how to enable location tracking in the background:
import BackgroundGeolocation from 'react-native-background-geolocation';

BackgroundGeolocation.configure({
  desiredAccuracy: BackgroundGeolocation.HIGH_ACCURACY,
  distanceFilter: 10,
  stopOnTerminate: false,
  startOnBoot: true,
  locationProvider: BackgroundGeolocation.ACTIVITY_PROVIDER,
  interval: 10000,
  fastestInterval: 5000,
  activitiesInterval: 10000,
  notificationTitle: 'Background tracking',
  notificationText: 'enabled',
  debug: false,
  startForeground: false,
});
  1. Geofencing: React Native Background Geolocation provides support for geofencing, which allows developers to set up virtual boundaries around specific locations. When a user enters or exits one of these boundaries, an event can be triggered. This functionality can be used for various purposes, such as sending notifications to users when they enter a specific location, triggering actions based on the user’s location, or providing location-based services.
  2. Power management: React Native Background Geolocation also provides robust power management features that help optimize battery usage when tracking a user’s location. This can be achieved by utilizing different location providers and adjusting the accuracy and frequency of location updates based on the user’s activity. The following code block demonstrates how to configure power management settings:
BackgroundGeolocation.configure({
  stopOnTerminate: false,
  startOnBoot: true,
  locationProvider: BackgroundGeolocation.ACTIVITY_PROVIDER,
  interval: 60000,
  fastestInterval: 30000,
  activitiesInterval: 10000,
});
Share

It’s Really not that Complicated.

You can actually understand what’s going on inside your live applications.

Try Lightrun’s Playground

Lets Talk!

Looking for more information about Lightrun and debugging?
We’d love to hear from you!
Drop us a line and we’ll get back to you shortly.

By clicking Submit I agree to Lightrun’s Terms of Use.
Processing will be done in accordance to Lightrun’s Privacy Policy.