Skip to content

Latest commit

 

History

History
117 lines (85 loc) · 4 KB

background.md

File metadata and controls

117 lines (85 loc) · 4 KB

Background Observers

An observer is responsible by constantly listen to Apple HealthKit updates in the background and notify your app in case any new data sample was added. The background notification is handled by iOS and the following steps show how to detect these changes using the react-native-health library.

Currently, the supported data identifiers that can be observed are the following:

  • ActiveEnergyBurned
  • BasalEnergyBurned
  • Cycling
  • InsulinDelivery
  • HeartRate
  • HeartRateVariabilitySDNN
  • RestingHeartRate
  • Running
  • StairClimbing
  • StepCount
  • Swimming
  • Vo2Max
  • Walking
  • Workout

Initialization

If you followed the Background Processing step in the README, you can skip this one.

To setup that in your project, in XCode open your ios/AppDelegate.m file and add the following statements:

#import "AppDelegate.h"

...

/* Add the library import at the top of AppDelegate.m */
#import "RCTAppleHealthKit.h"

...

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application
    didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

  RCTBridge *bridge = [[RCTBridge alloc] initWithDelegate:self
                                            launchOptions:launchOptions];

  ...

  /* Add Background initializer for HealthKit  */
  [[RCTAppleHealthKit new] initializeBackgroundObservers:bridge];

  ...

  return YES;
}

After that, your app is ready to start listening for data updates using the React Native client.

Handling the updates

This library send events to your app through the React Native bridge. To intercept those, you should use the NativeAppEventEmitter.

They follow events are triggered by the library

Event When is triggered?
healthKit:<OBSERVER_TYPE>:setup:success When the background observer for that type is successfuly setup
healthKit:<OBSERVER_TYPE>:setup:failure When the background observer for that type is not successfuly setup
healthKit:<OBSERVER_TYPE>:new When the background observer received a new data sample for that type
healthKit:<OBSERVER_TYPE>:failure When the background observer received a new data sample, but an error was found

Note that the <OBSERVER_TYPE> token should be replaced with one of the available types presented in the first section. As an example, if setting up observers for workouts, the events would have the following names:

  • healthKit:Workout:setup:success
  • healthKit:Workout:setup:failure
  • healthKit:Workout:new
  • healthKit:Workout:failure

Example

import React, { useEffect } from 'react';
import { NativeEventEmitter, NativeModules } from 'react-native';

useEffect(() => {
    new NativeEventEmitter(NativeModules.AppleHealthKit).addListener(
      'healthKit:HeartRate:new',
      async () => {
        console.log('--> observer triggered');
      },
    );
  });

When a new sample appears, in order to get the information you need to call the getSamples or the getClinicalRecords method from your callback function.

Note - Some HealthKit data types have a minimum update frequency of an hour. Even setting up an observer, it might take some moment until your app is notified by the HealthKit

More Information