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

Trying to schedule a notification based on the caloriesBurned only if they are less than a certain amount (with query to the health store) #686

Open
federicatopazio opened this issue Apr 3, 2023 · 6 comments

Comments

@federicatopazio
Copy link

Hi, I was trying to schedule a notification based on the calorie burned during the day by the app user, so that only if the user had burned during the day less than 300 kcal (for example) by 8 in the evening, it would have scheduled the notification and sent it by that hour, otherwise it would have done nothing. However, I have lots of difficulties managing the schedule of the notification everyday only if the condition is met, which should be verified with query to the health store. and that's where things get worse, since the query is asynchronous.
here is a snippet of the code In tried so far, but it stop on the first day, and never check the condition again, nor schedule the notification:
`// MARK: NOTIFICATION BASED ON HEALTHKIT DATA
func scheduleNotificationIfNeeded() {
let dispatchGroup = DispatchGroup()
let center = UNUserNotificationCenter.current()
let content = UNMutableNotificationContent()
content.title = "Reminder"
content.body = "You haven't burned enough calories today. Is that because you are experiencing severe pain? Come log it in!"
content.sound = .default

    let now = Date()
    let calendar = Calendar.current
    let year = calendar.component(.year, from: now)
    let month = calendar.component(.month, from: now)
    let day = calendar.component(.day, from: now)

    // Set the trigger date to 20:00 of the current day
    let dateComponents = DateComponents(year: year, month: month, day: day, hour: 20, minute: 0)
    let trigger = UNCalendarNotificationTrigger(dateMatching: dateComponents, repeats: false)


    let defaults = UserDefaults.standard
    if let lastScheduledDate = defaults.object(forKey: "lastScheduledNotificationDate") as? Date,
        Calendar.current.isDateInTomorrow(lastScheduledDate) {
        print("Notification already scheduled for tomorrow, skipping scheduling")
        return
    }

    dispatchGroup.enter()
    checkCaloriesBurned(forDate: Date()) { caloriesBurned in
        if caloriesBurned < 300 {
            let request = UNNotificationRequest(identifier: "CaloriesReminder", content: content, trigger: trigger)
            center.add(request) { error in
                if let error = error {
                    print("Error scheduling notification: \(error.localizedDescription)")
                } else {
                    let defaults = UserDefaults.standard
                    defaults.set(now, forKey: "lastScheduledNotificationDate")
                    print("Notification scheduled")
                }
            }
        } else {
            print("Calories burned is greater than or equal to 300, notification not scheduled")
            dispatchGroup.leave()
            DispatchQueue.main.asyncAfter(deadline: .now() + 24 * 60 * 60) {
                self.scheduleNotificationIfNeeded()
            }
        }
    }

    dispatchGroup.wait()
}

func checkCaloriesBurned(forDate date: Date, completion: @escaping (Double) -> Void) {
    let energyType = HKQuantityType.quantityType(forIdentifier: .activeEnergyBurned)!
    
    // Set the end date of the query to 20:00 of the current day
    let calendar = Calendar.current
    let endOfDay = calendar.date(bySettingHour: 20, minute: 0, second: 0, of: date)!
    let predicate = HKQuery.predicateForSamples(withStart: calendar.startOfDay(for: date), end: endOfDay, options: .strictEndDate)
    
    let query = HKStatisticsQuery(quantityType: energyType, quantitySamplePredicate: predicate, options: .cumulativeSum) { query, result, error in
        guard let result = result, let sum = result.sumQuantity() else {
            if let error = error {
                print("Error retrieving calories burned: \(error.localizedDescription)")
            }
            completion(0)
            return
        }
        let caloriesBurned = sum.doubleValue(for: HKUnit.kilocalorie())
        completion(caloriesBurned)
    }
    
    healthStore.execute(query)
}

`
@gavirawson-apple
Copy link
Collaborator

You might want to consider using the HealthKit linked tasks in CareKit. The task can be scheduled to occur every day, and will pull data from HealthKit to populate its outcome. The scheduling logic might look something like this:

func createCalorieTask() -> OCKHealthKitTask {

    let calorieLinkage = OCKHealthKitLinkage(
        quantityIdentifier: .activeEnergyBurned,
        quantityType: .cumulative,
        unit: .kilocalorie()
    )

    let startOfDay = Calendar.current.startOfDay(for: Date())

    let dailySchedule = OCKScheduleElement(
        start: startOfDay,
        end: nil,
        interval: DateComponents(day: 1),
        duration: .allDay,
        targetValues: [OCKOutcomeValue(300)]
    )

    let calorieTask = OCKHealthKitTask(
        id: "calories",
        title: "Burn Calories",
        carePlanUUID: nil,
        schedule: OCKSchedule(composing: [dailySchedule]),
        healthKitLinkage: calorieLinkage
    )

    return calorieTask
}

Now in terms of scheduling a notification, you can choose to either schedule or "unschedule" that 8PM notification whenever the outcome for the task changes.

var query = OCKEventQuery(for: anIntervalEncompassingAFewEvents)
query.taskIDs = ["calories"]

for events in store.anyEvents(matching: query) {

    // schedule or unschedule notifications based on the
    // progress for each event
}

@federicatopazio
Copy link
Author

Hi, thank you for your response. However I still have problems with the notification, since OCKEventQuery doesn't have a member taskIDs

@federicatopazio
Copy link
Author

I have problem with the schedule of this task you provided, and I'm struggling to understand how should I use to "schedule and unschedule" the notification based on the calories burned during the day

@gavirawson-apple
Copy link
Collaborator

Hi, thank you for your response. However I still have problems with the notification, since OCKEventQuery doesn't have a member taskIDs

Which branch of CareKit are you using? The main branch should have that property.

I have problem with the schedule of this task you provided

What problem are you running into?

I'm struggling to understand how should I use to "schedule and unschedule" the notification based on the calories burned during the day

You can begin by checking the progress for an event. If the progress is not completed, schedule a notification for 8PM. If the progress is completed, remove the 8PM notification.

@federicatopazio
Copy link
Author

`func createCalorieTask() -> OCKHealthKitTask {

let calorieLinkage = OCKHealthKitLinkage(
    quantityIdentifier: .activeEnergyBurned,
    quantityType: .cumulative,
    unit: .kilocalorie()
)

let startOfDay = Calendar.current.startOfDay(for: Date())

let dailySchedule = OCKScheduleElement(
    start: startOfDay,
    end: nil,
    interval: DateComponents(day: 1),
    duration: .allDay,
    targetValues: [OCKOutcomeValue(300)]
)

let calorieTask = OCKHealthKitTask(
    id: "calories",
    title: "Burn Calories",
    carePlanUUID: nil,
    schedule: OCKSchedule(composing: [dailySchedule]),
    healthKitLinkage: calorieLinkage
)

return calorieTask

}`
this task should be added to the healthkitstore right? And how do I check the progress to schedule the notification? and the OCKEventQuery(for: anIntervalEncompassingAFewEvents) gives me problem, too

Which branch of CareKit are you using? The main branch should have that property.
it must be 2.1 version

@gavirawson-apple
Copy link
Collaborator

this task should be added to the healthkitstore right?

Yep! The OCKHealthKitPassthroughStore or an OCKStoreCoordinator.

And how do I check the progress to schedule the notification?

On 2.1, fetch the event for the task and check to see if the outcome value matches the target value for the schedule (which looks to be 300 calories based on the snippet above). To check compare those two, see OCKAnyEvent.outcome and OCKAnyEvent.scheduleEvent

OCKEventQuery(for: anIntervalEncompassingAFewEvents) gives me problem

That initializer expects a single date, not a date interval. You can use this initializer instead - OCKEventQuery(dateInterval:)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants