DeviceActivity setup

Hi!

I'm playing around with the Screen Time API. I would like to let my app notify me every time I have used my device for 1 minute (any app). When I have been notified, I would like to reset the monitoring so that I will be notified again after another minute using my device.

I'm not sure how to accomplish this. In the main app, I currently start monitoring the following event with a schedule:

let event = DeviceActivityEvent(threshold: DateComponents(minute: 1))

let schedule = DeviceActivitySchedule(intervalStart: DateComponents(hour: 0, minute: 0, second: 1), intervalEnd: DateComponents(hour:23, minute: 59, second: 59), repeats: true)

Then, in the Device Activity Monitor Extension, I restart the same monitoring once the threshold is reached in eventDidReachThreshold.

However, eventDidReachThreshold is being called much more than once an hour. I have checked that I only have one monitoring event running, so duplicates does not seem to be a problem. So, my questions are:

  1. Does my approach look OK? What am I missing?
  2. Would this monitoring collect usage from all devices connected to the same Apple ID, i.e. my Mac, iPad, iPhones etc? If so, how can I make it only monitor usage on one of my devices (the iPhone running the app)?

However, eventDidReachThreshold is being called much more than once an hour.

This may be because you set your threshold to 1 minute in the line let event = DeviceActivityEvent(threshold: DateComponents(minute: 1)). Are you using the device for longer than a minute in any given hour?

Hi, I'm struggling to get anything to be called on the extension once a given threshold is reached. Would you be able to share your code for that? I'm specifically how you were able to get your extension to execute after an event.

DeviceActivity setup
 
 
Q