react-native-health
react-native-health copied to clipboard
Step and Heart Rate backgroundObservers aren't working as expected
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
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')
);
});
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);
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
@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
When calling getSamples in the callback I get an empty array as a response.
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?
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.
I have the same issue