DeviceActivityMonitor Extension Not Working

I added the extension via targets. And I added code in the intervalDidStart() override to print and send a notification. But it never seems to run. Am I supposed to do any other steps after adding the extension?

// Make sure that your class name matches the NSExtensionPrincipalClass in your Info.plist.
class DeviceActivityMonitorExtension: DeviceActivityMonitor {
    
    func getSelectionFromUserDefaults() -> FamilyActivitySelection {
        let defaults = UserDefaults.standard
        
        if let encodedData = defaults.data(forKey: "ScreenTimeSelection") {
            let decoder = PropertyListDecoder()
            if let selection = try? decoder.decode(FamilyActivitySelection.self, from: encodedData) {
                return selection
            }
        }
        
        return FamilyActivitySelection()
    }
    
    override func intervalDidStart(for activity: DeviceActivityName) {
        super.intervalDidStart(for: activity)
        
        print("HERE!")

        let content = UNMutableNotificationContent()
        content.title = "Feed the cat"
        content.subtitle = "It looks hungry"
        content.sound = UNNotificationSound.default

        // show this notification five seconds from now
        let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 5, repeats: false)

        // choose a random identifier
        let request = UNNotificationRequest(identifier: UUID().uuidString, content: content, trigger: trigger)

        // add our notification request
        UNUserNotificationCenter.current().add(request)
        
        let selection = getSelectionFromUserDefaults()
        
        let applications = selection.applicationTokens
        let categories = selection.categoryTokens
        let webCategories = selection.webDomainTokens

        let store = ManagedSettingsStore()

        store.shield.applications = applications.isEmpty ? nil : applications
        store.shield.applicationCategories = ShieldSettings.ActivityCategoryPolicy.specific(categories, except: Set())
        store.shield.webDomainCategories = ShieldSettings.ActivityCategoryPolicy.specific(categories, except: Set())
    }
Post not yet marked as solved Up vote post of DanteDoxo Down vote post of DanteDoxo
642 views
  • I was having a similar issue what helped me was putting the App and the DeviceActivityMonitorExtension in an AppGroup

Add a Comment

Replies

Did you you get a message saying "This scheme has been created for the “(null)” target." as I did in 735762?