-
Notifications
You must be signed in to change notification settings - Fork 444
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
Comments
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
} |
Hi, thank you for your response. However I still have problems with the notification, since OCKEventQuery doesn't have a member taskIDs |
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 |
Which branch of CareKit are you using? The main branch should have that property.
What problem are you running into?
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. |
`func createCalorieTask() -> OCKHealthKitTask {
}`
|
Yep! The
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
That initializer expects a single date, not a date interval. You can use this initializer instead - |
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
The text was updated successfully, but these errors were encountered: