agencyenterprise / react-native-health

A React Native package to interact with Apple HealthKit
MIT License
841 stars 232 forks source link

Background observer is not working #308

Open dipayanpalit15 opened 1 year ago

dipayanpalit15 commented 1 year ago

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

Screenshot 2023-04-17 at 6 48 50 PM Screenshot 2023-04-17 at 6 47 58 PM
ngoclamsn1998napa commented 1 year ago

I have same issue!

bewallyt commented 1 year ago

Did y'all figure out this issue?

Krupal5691 commented 1 year ago

Is there any update? same for me.

Krupal5691 commented 1 year ago

@dipayanpalit15 @bewallyt @GGGava Is background observer working for anyone?

GGGava commented 1 year ago

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

bewallyt commented 1 year ago

@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.

Krupal5691 commented 12 months ago

@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.

GGGava commented 12 months ago

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?

bewallyt commented 11 months ago

@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`
Krupal5691 commented 11 months ago

@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.

Krupal5691 commented 11 months ago

@GGGava @bewallyt I am trying for below obsevers StepCount Cycling HeartRate

mfagerdal commented 11 months ago

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.

KevinDeShawn commented 11 months ago

@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.

swellander commented 10 months ago

@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?

Krupal5691 commented 10 months ago

@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);
        });
maxperry commented 10 months ago

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.

Lemiex commented 7 months ago

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?

vishal957132 commented 4 months ago

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.

codemobii commented 3 months ago

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)