react-native-health icon indicating copy to clipboard operation
react-native-health copied to clipboard

[Question] Do "Background observers" work when the app is closed?

Open ghashi opened this issue 2 years ago • 2 comments

I've read the following links:

  • https://github.com/agencyenterprise/react-native-health/blob/master/docs/background.md
  • https://stackoverflow.com/questions/26375767/healthkit-background-delivery-when-app-is-not-running

I've integrated "react-native-health" and "background observers" seem to work when the app is in background. I'm testing on a simulator. When I kill the app, then the "background observers" don't seem to work.

From this link they should work, but I'm not sure if right now they are not working because a) I'm using a simulator to test this or b) the react-native bridge doesn't work when the app is killed (the native side may receive the new data, but the JS side is not active) or c) I did something wrong

So, do "Background observers" work even when the app is closed/terminated/killed?

ghashi avatar Apr 02 '22 13:04 ghashi

@ghashi i'm having the same issue. Have you made any progress here?

CaptainJeff avatar Apr 06 '22 22:04 CaptainJeff

@CaptainJeff I uploaded a test version on TestFlight and now I can confirm that "Background observers" work even when the app is closed/terminated/killed 🎉

It may take some time (here it was around 2 minutes), but it works

How I tested:

I created a code like this

    eventEmitter.addListener(`healthKit:${AnEvent}:new`, async (data) => {
      // SEND LOCAL NOTIFICATION

      // PERFORM AN HTTP REQUEST
    });

Both the notification and the http requests were successful

ghashi avatar Apr 18 '22 16:04 ghashi

@ghashi @CaptainJeff Background observer not working in case of killed state for me. Could you please share some sample code or doc which you referred? Also, is local notification need to add for this If i already have an api ?

KiranRadish avatar Jan 16 '23 10:01 KiranRadish

Could you please share some sample code or doc which you referred?

  • https://stackoverflow.com/questions/26375767/healthkit-background-delivery-when-app-is-not-running
  • https://github.com/agencyenterprise/react-native-health/blob/master/docs/background.md
  • https://stackoverflow.com/questions/26375767/healthkit-background-delivery-when-app-is-not-running

is local notification need to add for this If i already have an api ?

I'm not sure I understand your question, but I would say you don't need a "Local Notification".

ghashi avatar Jan 16 '23 13:01 ghashi

I am adding listener as below

NativeAppEventEmitter.addListener('healthKit:HeartRate:new', callBackgroundObserversHeart())
NativeAppEventEmitter.addListener('healthKit:StepCount:new', callBackgroundObserversStep())

I am getting this response every time when i add some data in health app

[HealthKit] New sample received from Apple HealthKit - HeartRate [native] Sending healthKit:HeartRate:new with no listeners registered.

KiranRadish avatar Jan 16 '23 14:01 KiranRadish

not sure what is wrong here,

  • updated AppDelegate file followed stackoverflow guide but still getting this message

[HealthKit] New sample received from Apple HealthKit - HeartRate [native] Sending healthKit:HeartRate:new with no listeners registered. [HealthKit] New sample from Apple HealthKit processed - HeartRate

KiranRadish avatar Jan 16 '23 14:01 KiranRadish

@KiranRadish I often do see in hte logs that same warning but I've come to the conclusion (hopefully someone else can confirm) that its an issue on debug when restarting the metro server and the measuremetns do actually come in. Have you tested to see if the measurements are received after receiving that warning?

CaptainJeff avatar Jan 16 '23 17:01 CaptainJeff

@KiranRadish wait I just saw that your last log was the measurement being received. So it seems like you are receiving the measurement, right?

CaptainJeff avatar Jan 16 '23 17:01 CaptainJeff

Hey @KiranRadish @CaptainJeff, regarding the "no listeners registered" warning, this is caused by NativeAppEventEmitter being deprecated. We updated the Background Observers docs with new instructions.

tldr: Add new listeners using NativeEventEmitter instead of NativeAppEventEmitter

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

GGGava avatar Jan 17 '23 13:01 GGGava

And yes, Background Observers should work when the app is closed, but note that it's not expected to trigger every time new data is written to Health:

HealthKit wakes your app whenever a process saves or deletes samples of the specified type. The system wakes your app at most once per time period defined by the specified frequency. Some sample types have a maximum frequency of HKUpdateFrequency.hourly. The system enforces this frequency transparently.

More info here

Also, background observers won't trigger if your device is locked, for privacy reasons.

Personally, I like to test the background observers by adding a workout sample to the Health app, they seem to trigger the observer almost every time.

GGGava avatar Jan 17 '23 13:01 GGGava

@GGGava Thanks for your quick update. Yes now with NativeEventEmitter its working exactly as expected.

KiranRadish avatar Jan 17 '23 14:01 KiranRadish

No @CaptainJeff i was not able to receive any data but it was showing processed log. After changing listener with NativeEventEmitter its working. Also doc is updated with latest version so anyone adding this background feature will get help from doc. Thanks @GGGava @CaptainJeff @ghashi

KiranRadish avatar Jan 17 '23 14:01 KiranRadish