Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Background observer is not working #308

Open
dipayanpalit15 opened this issue Apr 17, 2023 · 18 comments
Open

Background observer is not working #308

dipayanpalit15 opened this issue Apr 17, 2023 · 18 comments
Labels
bug Something isn't working

Comments

@dipayanpalit15
Copy link

dipayanpalit15 commented Apr 17, 2023

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 dipayanpalit15 added the bug Something isn't working label Apr 17, 2023
@ngoclamsn1998napa
Copy link

I have same issue!

@bewallyt
Copy link

bewallyt commented Aug 7, 2023

Did y'all figure out this issue?

@Krupal5691
Copy link

Is there any update? same for me.

@Krupal5691
Copy link

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

@GGGava
Copy link
Collaborator

GGGava commented Sep 5, 2023

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
Copy link

bewallyt commented Sep 5, 2023

@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
Copy link

@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
Copy link
Collaborator

GGGava commented Sep 11, 2023

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
Copy link

bewallyt commented Sep 14, 2023

@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
Copy link

@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
Copy link

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

@mfagerdal
Copy link

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
Copy link

@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
Copy link

swellander commented Nov 2, 2023

@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
Copy link

Krupal5691 commented Nov 3, 2023

@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
Copy link

maxperry commented Nov 9, 2023

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
Copy link

Lemiex commented Jan 27, 2024

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
Copy link

vishal957132 commented Apr 11, 2024

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests