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

Subscription and background delivery methods are not working

Open zain148 opened this issue 8 months ago • 3 comments

We're having a critical issue when we try to sync our watch app with our iOS app.

Whenever a watch app sends back data to the OS, it gets updated in the Health (iOS app). But in the case of our app, the subscription event never Triggers.

I'm not sure why this is not working as this looks like a very critical issue and without that our app will not function properly.

@robertherber can you please review it?

I've also tried to review in the brand new Expo SDK-52 .The issue is the same, I turn it off to newArchEnabled: false but the issue is still the same

System: OS: macOS 15.4.1 Shell: 5.9 - /bin/zsh Binaries: Node: 22.6.0 - /opt/homebrew/bin/node Yarn: 1.22.22 - /usr/local/bin/yarn npm: 10.8.2 - /opt/homebrew/bin/npm Watchman: 2024.08.05.00 - /opt/homebrew/bin/watchman Managers: CocoaPods: 1.15.2 - /usr/local/bin/pod SDKs: iOS SDK: Platforms: DriverKit 24.4, iOS 18.4, macOS 15.4, tvOS 18.4, visionOS 2.4, watchOS 11.4 IDEs: Android Studio: 2024.1 AI-241.18034.62.2411.12071903 Xcode: 16.3/16E140 - /usr/bin/xcodebuild npmPackages: expo: ~52.0.46 => 52.0.46 expo-router: ~4.0.20 => 4.0.20 react: 18.3.1 => 18.3.1 react-dom: 18.3.1 => 18.3.1 react-native: 0.76.9 => 0.76.9 react-native-web: ~0.19.13 => 0.19.13 npmGlobalPackages: eas-cli: 16.1.0 Expo Workflow: managed

zain148 avatar May 01 '25 06:05 zain148

@robertherber Just FYI: we waitied nearly 1 hour but it still didn't get updated the heartRate in our app.

zain148 avatar May 01 '25 07:05 zain148

Do you have a code example? How are you setting it up to work for your iOS app? Does the iOS app pick up general changes to the Health app (that are not from Watch)?

zoltlabs avatar May 03 '25 05:05 zoltlabs

@zoltlabs Here's the simple demo app created in react-native,

https://github.com/zain148/healthkit-issue.git

How are you setting it up to work for your iOS app? Ans: I have followed the docs and set up my project

Does the iOS app pick up general changes to the Health app (that are not from the Watch)? Yes, but it's not sync whenever the heart rate changes using subscription method.

zain148 avatar May 06 '25 12:05 zain148

is the background processing working?

Deveshb15 avatar May 13 '25 09:05 Deveshb15

@Deveshb15 for me it's working on old architecture. And doesn't with new one.

VladYakut avatar May 14 '25 13:05 VladYakut

Can you pls tell me what's your expo sdk version?

On Wed, 14 May 2025 at 6:30 PM VladYakut @.***> wrote:

VladYakut left a comment (kingstinct/react-native-healthkit#139) https://github.com/kingstinct/react-native-healthkit/issues/139#issuecomment-2880259679

@Deveshb15 https://github.com/Deveshb15 for me it's working on old architecture. And doesn't with new one.

— Reply to this email directly, view it on GitHub https://github.com/kingstinct/react-native-healthkit/issues/139#issuecomment-2880259679, or unsubscribe https://github.com/notifications/unsubscribe-auth/AKQTYR5LLKG4TO6R3OSPYYL26NAQTAVCNFSM6AAAAAB4HLFVYWVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDQOBQGI2TSNRXHE . You are receiving this because you authored the thread.Message ID: @.***>

zain148 avatar May 14 '25 13:05 zain148

@zain148 same - 52.0.46

VladYakut avatar May 14 '25 13:05 VladYakut

@zain148 your implementation looks incorrect since authStatus is not set when app wakes up - your code does not get executed.

Change requestPermissions to run automatically with useEffect.

rokanost avatar May 17 '25 01:05 rokanost

Permission was request correctly.

On Sat, 17 May 2025 at 6:40 AM Rocky S. @.***> wrote:

rokanost left a comment (kingstinct/react-native-healthkit#139) https://github.com/kingstinct/react-native-healthkit/issues/139#issuecomment-2887944107

@zain148 https://github.com/zain148 your implementation looks incorrect since authStatus is not set when app wakes up - your code does not get executed.

Change requestPermissions to run automatically with useEffect.

— Reply to this email directly, view it on GitHub https://github.com/kingstinct/react-native-healthkit/issues/139#issuecomment-2887944107, or unsubscribe https://github.com/notifications/unsubscribe-auth/AKQTYR4YTQE26ZJZXNNA4G3262HQ3AVCNFSM6AAAAAB4HLFVYWVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDQOBXHE2DIMJQG4 . You are receiving this because you were mentioned.Message ID: @.***>

zain148 avatar May 17 '25 14:05 zain148

@Deveshb15 Not for now.

zain148 avatar May 22 '25 01:05 zain148

@VladYakut can you please gave me a template code for your implementation, as it seems like even with old architecture, I'm still not able to run it.

zain148 avatar May 22 '25 01:05 zain148

In demo app it doesn't seem like u set up background observer? Just a normal observer

zoltlabs avatar May 23 '25 05:05 zoltlabs

Do you mean EnableBackgroundDeliver() function?

I already tried enabling this but that was also not working fine for me

On Fri, 23 May 2025 at 10:56 AM Zolt Labs @.***> wrote:

zoltlabs left a comment (kingstinct/react-native-healthkit#139) https://github.com/kingstinct/react-native-healthkit/issues/139#issuecomment-2903349887

In demo app it doesn't seem like u set up background observer? Just a normal observer

— Reply to this email directly, view it on GitHub https://github.com/kingstinct/react-native-healthkit/issues/139#issuecomment-2903349887, or unsubscribe https://github.com/notifications/unsubscribe-auth/AKQTYR4MFOLX4PYQ7F7NVTL2722ANAVCNFSM6AAAAAB4HLFVYWVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDSMBTGM2DSOBYG4 . You are receiving this because you were mentioned.Message ID: @.***>

zain148 avatar May 23 '25 12:05 zain148

Hello. Same for me. I use

  • "expo": "^53.0.9",
  • "react": "19.0.0",
  • "react-native": "0.79.2"

builded by xcode with background fetch, process, healthkit. app.js updated

      [
        "@kingstinct/react-native-healthkit",
        {
          "NSHealthUpdateUsageDescription": false,
          "healthkitBackgroundDelivery": true
        }
      ]

Code extract

const [hasRequestedAuthorization, setHasRequestedAuthorization] = useState(false);

const checkPermissions = async () => {
  try {
    const available = await isHealthDataAvailable();
    if (!available) throw new Error("HealthKit not available");

    const granted = await requestAuthorization([
      HKQuantityTypeIdentifier.stepCount,
      HKQuantityTypeIdentifier.distanceCycling,
      HKQuantityTypeIdentifier.distanceWalkingRunning,
      HKQuantityTypeIdentifier.distanceSwimming
    ]);

    if (!granted) throw new Error("Permissions not granted");
    else setHasRequestedAuthorization(true);

    console.log("granted", granted, "available", available)

  } catch (error) {
    console.error("Error :", error);
  }
};

useEffect(() => {
  let unsubscribe: (() => Promise<boolean>) | null = null;

  const setupSubscription = async () => {
    try {
      const granted = await requestAuthorization([
        HKQuantityTypeIdentifier.stepCount,
        HKQuantityTypeIdentifier.distanceCycling,
        HKQuantityTypeIdentifier.distanceWalkingRunning,
        HKQuantityTypeIdentifier.distanceSwimming
      ]);

      if (!granted) return;

      await enableBackgroundDelivery(
        HKQuantityTypeIdentifier.stepCount,
        HKUpdateFrequency.immediate
      );

      console.log("------Setup event");
      unsubscribe = await subscribeToChanges(
        HKQuantityTypeIdentifier.stepCount,
        async () => {
          console.log("<<<<<<<<<<<<<<<<NEWEVENT");
        }
      );
    } catch (error) {
      console.error('Error:', error);
    }
  };

  if (hasRequestedAuthorization) {
    setupSubscription();
  }

  return () => {
    unsubscribe?.();
  };
}, [hasRequestedAuthorization]);

But I never see my console.log NEWEVENT after adding manually a new countStep on the Apple health app. However, I can manually fetch health data.

Mat-vipa avatar Jun 04 '25 16:06 Mat-vipa

This is awesome! When should we expect it to be released?

nzapponi avatar Jun 10 '25 21:06 nzapponi