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

Background observer is not working

Open dipayanpalit15 opened this issue 1 year ago • 20 comments

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
  • Device: iPhone 14 pro
  • OS: 16.4.1
  • Version : RN 0.67.3 and react-native-health - 1.14.0

dipayanpalit15 avatar Apr 17 '23 13:04 dipayanpalit15

I have same issue!

ngoclamsn1998napa avatar Jul 04 '23 07:07 ngoclamsn1998napa

Did y'all figure out this issue?

bewallyt avatar Aug 07 '23 19:08 bewallyt

Is there any update? same for me.

Krupal5691 avatar Sep 05 '23 14:09 Krupal5691

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

Krupal5691 avatar Sep 05 '23 14:09 Krupal5691

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

GGGava avatar Sep 05 '23 14:09 GGGava

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

bewallyt avatar Sep 05 '23 19:09 bewallyt

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

Krupal5691 avatar Sep 11 '23 08:09 Krupal5691

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?

GGGava avatar Sep 11 '23 15:09 GGGava

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

bewallyt avatar Sep 14 '23 06:09 bewallyt

@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 avatar Sep 14 '23 13:09 Krupal5691

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

Krupal5691 avatar Sep 15 '23 09:09 Krupal5691

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 avatar Sep 19 '23 08:09 mfagerdal

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

KevinDeShawn avatar Sep 19 '23 11:09 KevinDeShawn

@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 avatar Nov 02 '23 23:11 swellander

@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);
        });

Krupal5691 avatar Nov 03 '23 03:11 Krupal5691

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.

maxperry avatar Nov 09 '23 22:11 maxperry

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?

Lemiex avatar Jan 27 '24 21:01 Lemiex

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.

vishal957132 avatar Apr 11 '24 05:04 vishal957132

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)

codemobii avatar Jun 06 '24 09:06 codemobii