iOS

Important notes about background delivery on iOS

  • For most data types the most possible frequency of updates is 1 hour.
  • iOS can update data more frequently for some data types, for example, vo2 max.
  • iOS may throttle the frequency of updates for background delivery depending on the app’s activity, battery state, etc.
  • Background delivery is not possible while device is locked, so it will be executed only when the device is unlocked.
  • iOS may stop background delivery if it detects that the app is not active for a long time.
  • The feature is available starting with iOS 15.

Setup

Enable background delivery for app target

  • Open XCode with your ios project
  • Open the folder of your project in Xcode
  • Select the project name in the left sidebar
  • Open Signing & Capabilities section
  • Select HealthKit background delivery under HealthKit section

Initialization at app startup

For background delivery to work properly, you need to initialize the SpikeSDK at app startup.

AppDelegate.swift:

import SpikeSDK
...
  override func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
    ...
    Spike.configure()
    ...
  }
...

or AppDelegate.mm:

#import <SpikeSDK/SpikeSDK-Swift.h>
...
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
 ...
 [Spike configure];
 ...

}

Android

Important notes about background delivery on Android

  • Background delivery is scheduled to run every hour. But ultimately Android decides when the delivery will be executed.
  • Android may throttle the frequency of updates for background delivery depending on the app’s activity, battery state, etc.
  • Android may stop background delivery if it detects that the app is not active for a long time.
  • There is a limit of queries that can be done in Health Connect and it is different for foreground and background reads, so please request only essential data to be delivered in background. More information on (Health Connect documentation)

Setup

Add the following permission to your AndroidManifest.xml:

<uses-permission android:name="android.permission.health.READ_HEALTH_DATA_IN_BACKGROUND" />

React Native specific

Add includeBackgroundDelivery: true when asking for permissions:

spikeConnection.requestHealthPermissions({
    statisticTypes: statisticTypes,
    includeBackgroundDelivery: true,
})

You can also ask for background delivery permission at the same time as other permissions.

Now you can enable background delivery:

try {
    await spikeConnection.enableBackgroundDelivery({
        statisticTypes: [StatisticsType.steps, StatisticsType.distanceTotal],
        sleepConfigs: [new SleepConfig({
            includeMetricTypes: [], 
        })],
    })

    toast.show("Background delivery enabled")
    } catch (error) {
    console.log(`${error}`);
    toast.show(`${error}`);
} catch (error) {
    console.log(`${error}`);
}

Keep in mind that calling enableBackgroundDelivery() will overwrite previous configuration. So you have to call it with all the data types you want in one call.

To check current configuration call getBackgroundDeliveryConfig() method.

To stop background delivery call disableBackgroundDelivery() method.