Skip to main content

Usage: Sync health data automatically

Schedule automatic syncs with every app launch.

Yesterday sync

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:

scheduleYesterdaySync requires the following parameters:

@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. Also, this MUST NOT be used if you are already using the rookYesterdaySync delegate with the native implementation.

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 are meet:

  • The device battery is low
  • The device storage is low
  • The device is not connected to the internet
  • The user hasn't granted Android Permissions (POST_NOTIFICATIONS, FOREGROUND_SERVICE, FOREGROUND_SERVICE_HEALTH)
  • The device has previously exceeded the Health Connect request quota and the recovery timestamp hasn't been meet
  • The user hasn't granted Health Connect Permissions (SLEEP, PHYSICAL, BODY)
  • The most recent request exceeded the Health Connect request quota
  • The userID hasn't been configured
  • There is an error initializing the SDK