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

Step and Heart Rate backgroundObservers aren't working as expected

Open hammad0316 opened this issue 3 years ago • 5 comments

Describe the bug The StepCount and HeartRate background observers seem to not be working as expected

Expected behavior If a new step count or heart rate entry is added into apple health, the observer function should fire off and be called/ran

hammad0316 avatar Feb 25 '21 19:02 hammad0316

This is how I'm setting up the event listeners, only one that gets triggered correctly in the background is workout. I'm also not seeing any messages for the set up call backs:

const syncOptions = [
        'StepCount',
        'HeartRate',
        'Workout'
      ];

syncOptions.forEach(option => {
        NativeAppEventEmitter.addListener(
          `healthKit:${option}:setup:failure`,
          () => console.log(option, 'setup failure')
        );

        NativeAppEventEmitter.addListener(
          `healthKit:${option}:setup:success`,
          () => console.log(option, 'setup success')
        );

        NativeAppEventEmitter.addListener(`healthKit:${option}:new`, () =>
          // queryForAppleHealth(true)
          console.log(option, 'triggered new')
        );

        NativeAppEventEmitter.addListener(`healthKit:${option}:failure`, () =>
          console.log(option, 'triggered failure')
        );
      });

hammad0316 avatar Feb 25 '21 20:02 hammad0316

In my case I can listen to "healthKit:HeartRate:new", but don't know how to get the HeartRate, do I really have to use AppleHealthKit.getSamples inside my callback function? Inside an AppleHealthKit.initHealthKit?

Beside that, success and failure are not triggering: NativeAppEventEmitter.addListener('healthKit:HeartRate:setup:success', success); NativeAppEventEmitter.addListener('healthKit:HeartRate:setup:failure', failure);

terrorpixel avatar Mar 15 '21 16:03 terrorpixel

Hey @hammad0316! 👋🏿

Thanks for letting us know. It seems that you are setting everything up correctly on the React Native side. Just in order to gather more context, are you running on a simulator or a real device?

Also, if you run your app in XCode, you will be able to see in the native logs when a new sample is received by the library. It will have the following format:

[HealthKit] New sample received from Apple HealthKit - HeartRate

Can you check whether this log is being sent when you add a new sample in Apple Health?

Also worth mentioning that Apple Health has rules on the frequency in which some samples are sent to the app using background observers. See more

lucaspbordignon avatar Mar 18 '21 12:03 lucaspbordignon

@terrorpixel

Yes! In summary, the listener only notifies you when we have a new sample added to Apple Health, but it does not return you the added samples. You should call getSamples() to fetch them

lucaspbordignon avatar Mar 18 '21 12:03 lucaspbordignon

When calling getSamples in the callback I get an empty array as a response.

maitham avatar Jul 08 '21 23:07 maitham

I have added Listener as NativeAppEventEmitter.addListener('healthKit:StepCount:new', callBackgroundObserversStep())

and @lucaspbordignon i am getting below response in xcode

[native] Sending healthKit:StepCount:new with no listeners registered.

can you please suggest whats wrong here?

KiranRadish avatar Jan 16 '23 12:01 KiranRadish

This issue has been inactive for a long time and we recently did some changes/fixes on the observers. I will close this issue, please let me know if the problem persists.

GGGava avatar Jan 23 '23 13:01 GGGava

I have the same issue

Doankimtuan avatar Mar 21 '23 04:03 Doankimtuan