react-native-health
react-native-health copied to clipboard
Background observer is not working
I'm trying to implement background observers but it's not working.Followed updated documents for Background observers but still same issue.
Expected behavior Should trigger the events for background observers.
Screenshots
This is my App.js file


- Device: iPhone 14 pro
- OS: 16.4.1
- Version : RN 0.67.3 and react-native-health - 1.14.0
I have same issue!
Did y'all figure out this issue?
Is there any update? same for me.
@dipayanpalit15 @bewallyt @GGGava Is background observer working for anyone?
Hey, I'll take a look on this tomorrow. But let me ask: Is the background capability on? Does the observer work when the app is on foreground? Thanks
@Krupal5691
Try running with the dev build & with the phone plugged in (i.e., charging) and add data manually to HK and see if the observers trigger. Running the production .ipa or even the dev build without the phone charging will limit how often the background observer queries for HK events given Apple's algorithm.
That's my understanding at least.
@GGGava Background capability is on. Observers does not work even when app is on foreground.
@bewallyt observers didn't work even when phone was connected to macbook and it was charging. Added data to HK manually but that didn't work. observers didn't trigger in both simulator, real device. Also check in both dev and production build but not triggered.
Hey @dipayanpalit15 tested it locally and it seems the issue is with the Cycling
observer. Not sure what is going on, I'll take a look later, but you can just use the Workout
event: 'healthKit:Workout:new'
. Cycling is a Workout, so works the same.
Let me know if the issue persists.
@Krupal5691 is your issue similar? What observer are you trying to use?
@Krupal5691 run the dev build thru xcode and add health data (e.g., create a workout event) in the apple health app. you should at the very least see the logs being sent thru xcode
like this
Sending `healthKit:MindfulSession:new` with no listeners registered.
[HealthKit] New sample from Apple HealthKit processed - MindfulSession healthKit:MindfulSession:new
[HealthKit] New sample received from Apple HealthKit - SleepAnalysis
make sure you're registering all events like this:
[`healthKit:${observerType}:new`]: async () => {
console.log(`${observerType}: in newSampleObserver - received a new sample`);
},
[`healthKit:${observerType}:failure`]: () => {
console.log(`${observerType}: in newSampleObserver - new event failure`);
},
[`healthKit:${observerType}:setup:success`]: () => {
console.log(`${observerType}: in newSampleObserver - setup success`);
},
[`healthKit:${observerType}:setup:failure`]: () => {
console.log(`${observerType}: in newSampleObserver - setup failure`);
},
where observerType is `Workout`
@GGGava @bewallyt Observers are triggering while app is in foreground, I am getting log in console. But when I try it when app is in background, there is no log for observer, instead all that logs are coming when I open the app from background.
@GGGava @bewallyt I am trying for below obsevers StepCount Cycling HeartRate
Hi, I have a similar issue and have narrowed it down to the following When setting AppleHealthKit.initHealthKit, it works - I will be able to get data - any data which I have got permissions allowed for.
If I set background observers, following new NativeEventEmitter(NativeModules.AppleHealthKit).addListener in scenarios before, under (in delegate) or after permission asked - the observers will not fire.
HOWEVER, if I terminate the app and then reset/re-init the observers, now the notifications will fire in all cases.
That is not optimal ofcourse.
@mfagerdal I noticed the same behaviour just like the way you explained it. In addition to that I archived a build from the same code and uploaded it to TestFlight and its observers are not working at all. Not even after terminating the app and then reset/re-init the observers.
@mfagerdal I noticed the same behaviour just like the way you explained it. In addition to that I archived a build from the same code and uploaded it to TestFlight and its observers are not working at all. Not even after terminating the app and then reset/re-init the observers.
I'm experiencing the same behavior and haven't been able to get any type of listener to trigger while the app is in the background. Has anyone had any success with this?
@swellander @KevinDeShawn Now I am able to trigger the observers in background mode even if app is killed by user. Also able to upload data to the server. Tested with TestFlight.
I have moved listener from App.js and placed it inside AppleHealthKit initialisation. Also upgraded react native version.
React Native : 0.72.4
AppleHealthKit.initHealthKit(this._options, (err, result) => {
if (err) {
resolve(false);
return;
}
new NativeEventEmitter(NativeModules.AppleHealthKit).addListener(
'healthKit:StepCount:new',
async () => {
try {
await trackerService.retrieveAndSendAllHealthDataFrom(0)
} catch (e){
}
},
);
new NativeEventEmitter(NativeModules.AppleHealthKit).addListener(
'healthKit:Cycling:new',
async () => {
try {
await trackerService.retrieveAndSendAllHealthDataFrom(0)
} catch (e){
}
},
);
new NativeEventEmitter(NativeModules.AppleHealthKit).addListener(
'healthKit:HeartRate:new',
async () => {
try {
await trackerService.retrieveAndSendAllHealthDataFrom(0)
} catch (e){
}
},
);
new NativeEventEmitter(NativeModules.AppleHealthKit).addListener(
'healthKit:Workout:new',
async () => {
try {
await trackerService.retrieveAndSendAllHealthDataFrom(0)
} catch (e){
}
},
);
new NativeEventEmitter(NativeModules.AppleHealthKit).addListener(
'healthKit:StepCount:setup:success',
async () => {
// console.log('--> StepCount observer NativeEventEmitter setup success');
},
);
new NativeEventEmitter(NativeModules.AppleHealthKit).addListener(
'healthKit:Cycling:setup:success',
async () => {
// console.log('--> Cycling observer NativeEventEmitter setup success');
},
);
new NativeEventEmitter(NativeModules.AppleHealthKit).addListener(
'healthKit:HeartRate:setup:success',
async () => {
// console.log('--> HeartRate observer NativeEventEmitter setup success');
},
);
new NativeEventEmitter(NativeModules.AppleHealthKit).addListener(
'healthKit:Workout:setup:success',
async () => {
// console.log('--> Workout observer NativeEventEmitter setup success');
},
);
new NativeEventEmitter(NativeModules.AppleHealthKit).addListener(
'healthKit:StepCount:setup:failure',
async () => {
// console.log('--> StepCount observer NativeEventEmitter setup failure');
},
);
new NativeEventEmitter(NativeModules.AppleHealthKit).addListener(
'healthKit:Cycling:setup:failure',
async () => {
// console.log('--> Cycling observer NativeEventEmitter setup failure');
},
);
new NativeEventEmitter(NativeModules.AppleHealthKit).addListener(
'healthKit:HeartRate:setup:failure',
async () => {
// console.log('--> HeartRate observer NativeEventEmitter setup failure');
},
);
new NativeEventEmitter(NativeModules.AppleHealthKit).addListener(
'healthKit:Workout:setup:failure',
async () => {
// console.log('--> Workout observer NativeEventEmitter setup failure');
},
);
new NativeEventEmitter(NativeModules.AppleHealthKit).addListener(
'healthKit:StepCount:failure',
async () => {
// console.log('--> StepCount observer NativeEventEmitter failure');
},
);
new NativeEventEmitter(NativeModules.AppleHealthKit).addListener(
'healthKit:Cycling:failure',
async () => {
// console.log('--> Cycling observer NativeEventEmitter failure');
},
);
new NativeEventEmitter(NativeModules.AppleHealthKit).addListener(
'healthKit:HeartRate:failure',
async () => {
// console.log('--> HeartRate observer NativeEventEmitter failure');
},
);
new NativeEventEmitter(NativeModules.AppleHealthKit).addListener(
'healthKit:Workout:failure',
async () => {
// console.log('--> Workout observer NativeEventEmitter failure');
},
);
resolve(true);
});
Hi, I have a similar issue and have narrowed it down to the following When setting AppleHealthKit.initHealthKit, it works - I will be able to get data - any data which I have got permissions allowed for.
If I set background observers, following new NativeEventEmitter(NativeModules.AppleHealthKit).addListener in scenarios before, under (in delegate) or after permission asked - the observers will not fire.
HOWEVER, if I terminate the app and then reset/re-init the observers, now the notifications will fire in all cases.
That is not optimal ofcourse.
@mfagerdal this is happening for me too. @Krupal5691's solution didn't work either. Observers only start working after allowing permissions and relaunching the app.
Facing the same issue... I'm surprised that such a crucial feature doesn't seem to work for the average person trying it. Is this a bug or is there a specific method for using the background observer that isn't well documented?
I am also facing the same issue, tried healthKit:StepCount:new, healthKit:Cycling:new but no success. Then I tried healthKit:Workout:new and then app triggers when we add workout session in health app manually. What I faced is when I manually adding steps/cycling/running/walking/SleepAnalysis but app does not triggers in background or in killed state. Then I tried to add cycling/running/walking from workout session then it triggers.
new NativeEventEmitter(NativeModules.AppleHealthKit).addListener( 'healthKit:StepCount:new', async () => { try { await trackerService.retrieveAndSendAllHealthDataFrom(0) } catch (e){ } }, ); new NativeEventEmitter(NativeModules.AppleHealthKit).addListener( 'healthKit:Cycling:new', async () => { try { await trackerService.retrieveAndSendAllHealthDataFrom(0) } catch (e){ } }, ); new NativeEventEmitter(NativeModules.AppleHealthKit).addListener( 'healthKit:HeartRate:new', async () => { try { await trackerService.retrieveAndSendAllHealthDataFrom(0) } catch (e){ } }, ); new NativeEventEmitter(NativeModules.AppleHealthKit).addListener( 'healthKit:Workout:new', async () => { try { await trackerService.retrieveAndSendAllHealthDataFrom(0) } catch (e){ } }, );
new NativeEventEmitter(NativeModules.AppleHealthKit).addListener( 'healthKit:StepCount:setup:success', async () => { // console.log('--> StepCount observer NativeEventEmitter setup success'); }, ); new NativeEventEmitter(NativeModules.AppleHealthKit).addListener( 'healthKit:Cycling:setup:success', async () => { // console.log('--> Cycling observer NativeEventEmitter setup success'); }, ); new NativeEventEmitter(NativeModules.AppleHealthKit).addListener( 'healthKit:HeartRate:setup:success', async () => { // console.log('--> HeartRate observer NativeEventEmitter setup success'); }, ); new NativeEventEmitter(NativeModules.AppleHealthKit).addListener( 'healthKit:Workout:setup:success', async () => { // console.log('--> Workout observer NativeEventEmitter setup success'); }, ); new NativeEventEmitter(NativeModules.AppleHealthKit).addListener( 'healthKit:StepCount:setup:failure', async () => { // console.log('--> StepCount observer NativeEventEmitter setup failure'); }, ); new NativeEventEmitter(NativeModules.AppleHealthKit).addListener( 'healthKit:Cycling:setup:failure', async () => { // console.log('--> Cycling observer NativeEventEmitter setup failure'); }, ); new NativeEventEmitter(NativeModules.AppleHealthKit).addListener( 'healthKit:HeartRate:setup:failure', async () => { // console.log('--> HeartRate observer NativeEventEmitter setup failure'); }, ); new NativeEventEmitter(NativeModules.AppleHealthKit).addListener( 'healthKit:Workout:setup:failure', async () => { // console.log('--> Workout observer NativeEventEmitter setup failure'); }, ); new NativeEventEmitter(NativeModules.AppleHealthKit).addListener( 'healthKit:StepCount:failure', async () => { // console.log('--> StepCount observer NativeEventEmitter failure'); }, ); new NativeEventEmitter(NativeModules.AppleHealthKit).addListener( 'healthKit:Cycling:failure', async () => { // console.log('--> Cycling observer NativeEventEmitter failure'); }, ); new NativeEventEmitter(NativeModules.AppleHealthKit).addListener( 'healthKit:HeartRate:failure', async () => { // console.log('--> HeartRate observer NativeEventEmitter failure'); }, ); new NativeEventEmitter(NativeModules.AppleHealthKit).addListener( 'healthKit:Workout:failure', async () => { // console.log('--> Workout observer NativeEventEmitter failure'); }, );
Hello, Is this still working for you? And please can I see your implementation for await trackerService.retrieveAndSendAllHealthDataFrom(0)