DeviceActivityMonitor event threshold callbacks often triggered multiple times in a row

My app sends screen time awareness notifications based on DeviceActivityMonitor thresholds.

Often, users receive two notifications in a row for the same screen time threshold. This means that the app extension is triggering the same eventDidReachThreshold callback function twice for the same threshold. I've made sure that there is only one activity schedule being monitored. This happens often, but not every time (over 50% of the time).

Anybody else experience this issue, and any way to mitigate it?

Replies

It's possible that the function fails before it returns and is retried. Does anyone know the retry policy for DeviceActivityMonitor override functions?

By any chance you're recursively setting another threshold monitoring upon threshold reached?

  • I'm not - I saw your reply to https://developer.apple.com/forums/thread/741810 so I understand why you're asking that. I also make sure not to send notifications if intervalDidStart was called recently (indicating the schedule was re-started) and that has been tested thoroughly, so that is not the issue.

  • However, I do interact with UserDefaults in the callback functions. And sometimes actions that are meant to be performed in the callback functions do not occur, indicating failure. I think it's likely that the callback functions are failing at some point and being retried, and so notifications get sent multiple times. But it's very difficult to debug these situations because of limited access to the app extension. Any advice?

  • I see. Thank you for the info! I've never encountered this issue though. I suppose you could try removing unnecessary components within the extension code, like UserDefaults, and see if the issue persists to decide which may be the root cause.

@charleswinston if you get a chance, please help answer some of the questions asking how to get the callback to run in the first place :)