react-native-background-fetch

repository·master·Indexed 23 days ago

https://github.com/transistorsoft/react-native-background-fetch

An iOS and Android BackgroundFetch API implementation for React Native (version 4.4.2). This plugin wakes up a React Native app approximately every 15 minutes to execute a callback function. It supports HeadlessJS on Android for handling events after app termination and includes a scheduleTask method for one-shot or periodic tasks. Compatible with React Native 0.85+ and Expo SDK 54+.

Tokens
7.3K
Snippets
23
Records
46
Agent score
81%

What's inside react-native-background-fetch

  1. Platform-specific behavior for iOS

    master

    When using Background Fetch on iOS, be aware of the following platform behaviors:

    • App Termination: If your app is terminated, iOS will not fire events. There is no stopOnTerminate: false option available for iOS.
    • Event Frequency: iOS uses a machine-learning algorithm to determine event frequency. It can take days for this algorithm to settle into a regular pattern. If your simulated events work, your configuration is likely correct.
    • User Inactivity: If a user does not open your iOS app for long periods, iOS may stop firing events entirely.
    • Scheduled Tasks: The scheduleTask method appears to only fire when the device is plugged into power.
  2. How Background Fetch works

    master

    Background Fetch attempts to awaken your app in the background approximately every 15 minutes to provide a short period of background running time. When a fetch event occurs, the plugin executes a provided callbackFn.

    Key Constraints:

    • Minimum Interval: You cannot increase the frequency of events. The plugin is set to the most frequent possible rate, and you will never receive an event faster than every 15 minutes.
    • OS Throttling: The operating system automatically throttles the frequency of events based on usage patterns (e.g., if the device hasn't been used for a long time, or if an iOS user disables background refresh).
    • Task Scheduling: The plugin now includes a scheduleTask method for scheduling arbitrary "one-shot" or periodic tasks.
  3. Configure app.json for Expo

    master

    To enable react-native-background-fetch in an Expo project, you must update your app.json with the following configurations:

    1. Add react-native-background-fetch to the plugins array.
    2. Configure ios.infoPlist to include the required UIBackgroundModes (fetch and processing) and BGTaskSchedulerPermittedIdentifiers (com.transistorsoft.fetch).

    If you plan to use custom tasks via BackgroundFetch.scheduleTask, you must also add those specific task identifiers to the BGTaskSchedulerPermittedIdentifiers list.

    {
      "expo": {
        "name": "your-app-name",
        "plugins": [
          "react-native-background-fetch"
        ],
        "ios": {
          "infoPlist": {
            "UIBackgroundModes": [
              "fetch",
              "processing"
            ],
            "BGTaskSchedulerPermittedIdentifiers": [
              "com.transistorsoft.fetch"
            ]
          }
        }
      }
    }
  4. Configure AppDelegate.m for iOS 13+ (BGTaskScheduler)

    master

    The BGTaskScheduler API introduced in iOS 13 requires you to register BackgroundFetch in your AppDelegate.m file.

    Add the import and call didFinishLaunching within didFinishLaunchingWithOptions:

    +#import <TSBackgroundFetch/TSBackgroundFetch.h>
    
    @implementation AppDelegate
    
    (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
      .
      .
    + // [REQUIRED] Register BackgroundFetch
    + [[TSBackgroundFetch sharedInstance] didFinishLaunching];
    
      return YES;
    }
  5. Configure Android Gradle settings

    master

    For manual installation on Android, you must include the project in your android/settings.gradle file so Gradle can locate the module within your node_modules directory.

    +include ':react-native-background-fetch'
    +project(':react-native-background-fetch').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-background-fetch/android')
  6. Configure Framework Search Paths for TSBackgroundFetch

    master

    To ensure Xcode can locate TSBackgroundFetch.framework, you must update your Build Settings:

    1. Go to the Build Settings tab in Xcode.
    2. Search for "framework search path".
    3. Add the following path and ensure recursive is selected: $(PROJECT_DIR)/../node_modules/react-native-background-fetch/ios
    $(PROJECT_DIR)/../node_modules/react-native-background-fetch/ios