Skip to main content

Usage: Sync health data automatically

Schedule automatic syncs with every app launch.

Yesterday sync

HCRookYesterdaySyncManager allows to launch 29 days historic health data synchronizations.

The following table describes Yesterday sync frequency of synchronization and historical range of each Health structure:

Health structureFrequencyHistorical range
Sleep SummaryEach time scheduleYesterdaySync is called29 days in the past until yesterday
Body SummaryEach time scheduleYesterdaySync is called29 days in the past until yesterday
Physical SummaryEach time scheduleYesterdaySync is called29 days in the past until yesterday
Physical ActivityEach time scheduleYesterdaySync is called29 days in the past until today
StepsEach time scheduleYesterdaySync is calledToday

To sync Health Data automatically you can use the scheduleYesterdaySync method in your app's initialization phase. The following example uses the focus_detector package:

@override
Widget build(BuildContext context) {
return FocusDetector(
onFocusGained: attemptToEnableYesterdaySync,
child: Column(
children: [
],
),
);
}

void attemptToEnableYesterdaySync() {
SharedPreferences.getInstance().then((prefs) {
final userAcceptedYesterdaySync = prefs.getBool("ACCEPTED_YESTERDAY_SYNC") ?? false;

if (userAcceptedYesterdaySync) {
HCRookYesterdaySyncManager.scheduleYesterdaySync(
enableNativeLogs: isDebug,
clientUUID: Secrets.clientUUID,
secretKey: Secrets.secretKey,
environment: rookEnvironment,
);
} else {
// The user did not accept the yesterday sync feature
}
});
}
tip

We recommend you to ask your users if they want to enable this behaviour, then save their preference in local storage and call scheduleYesterdaySync conditionally.

info

Once started rookYesterdaySync will attempt to sync all historic data, and it will shut down itself after it finishes or until the Health Connect request quota is exceeded.

danger

scheduleYesterdaySync is intended to be called once in your app, preferably once the user has opened the app as part of the application initialization phase.

Permissions

To use rookYesterdaySync you will need:

info

scheduleYesterdaySync also requires a user id, so it's possible that the first time you launch your app nothing will happen, only after a user id is configured and all necessary permissions are granted the automatic sync will happen the next time the app is launched.

Customizing the foreground service notification

To sync health data automatically a Foreground Service is used, this service requires a notification to be displayed until the synchronization finishes.

To use your own resources you need to reference them in the AndroidManifest.xml file:


<manifest xmlns:android="http://schemas.android.com/apk/res/android">
<application>
<meta-data
android:name="io.tryrook.service.notification.SYNC_ICON"
android:resource="@drawable/my_custom_icon"/>

<meta-data
android:name="io.tryrook.service.notification.SYNC_TITLE"
android:resource="@string/my_custom_title"/>

<meta-data
android:name="io.tryrook.service.notification.SYNC_CONTENT"
android:resource="@string/my_custom_content"/>
</application>
</manifest>
info

Starting on Android 13 (SDK 33) this notification can be dismissed without finishing the service associated with it, then the service will be displayed in the active apps section (This may vary depending on device brand).

Launch/Stop conditions

scheduleYesterdaySync won't start or will stop if one the following conditions is true:

  • The device battery is low.
  • The device storage is low.
  • The device is not connected to the internet.
  • The userID hasn't been configured.
  • The most recent request exceeded the Health Connect request quota.
  • The device has previously exceeded the Health Connect request quota and less than 60 minutes have passed.
  • The user hasn't granted Health Connect Permissions.
  • There is an error initializing the SDK.
  • The user hasn't granted Android Permissions.