Skip to content

Commit

Permalink
Add sendNotification to shieldActions
Browse files Browse the repository at this point in the history
  • Loading branch information
robertherber committed Jan 11, 2025
1 parent 08f645c commit a6f70fe
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 43 deletions.
12 changes: 6 additions & 6 deletions example/screens/ShieldTab.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,6 @@ export function ShieldTab() {
}
}, [familyActivitySelectionResult?.familyActivitySelection]);

const theme = useTheme();

const onSelectionChange = useCallback(
(
event: NativeSyntheticEvent<{
Expand Down Expand Up @@ -80,6 +78,8 @@ export function ShieldTab() {
blue: 0,
},
subtitle: "subtitle",
primaryButtonLabel: "primaryButtonLabel",
secondaryButtonLabel: "secondaryButtonLabel",
subtitleColor: {
red: Math.random() * 255,
green: Math.random() * 255,
Expand All @@ -103,19 +103,19 @@ export function ShieldTab() {
},
{
primary: {
type: "unblockAll",
behavior: "defer",
type: "openApp",
behavior: "close",
},
secondary: {
type: "dismiss",
behavior: "close",
behavior: "defer",
},
},
),
[shieldTitle],
);

const [showSelectionView, setShowSelectionView] = useState(true);
const [showSelectionView, setShowSelectionView] = useState(false);

return (
<SafeAreaView style={{ flex: 1 }}>
Expand Down
26 changes: 19 additions & 7 deletions ios/Shared.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import FamilyControls
import Foundation
import ManagedSettings
import UIKit
import WebKit
import os

var appGroup = "group.ActivityMonitor"
Expand Down Expand Up @@ -124,19 +125,30 @@ func openUrl(urlString: String) {
return // be safe
}

/*let context = NSExtensionContext()
context.open(url) { success in
let context = NSExtensionContext()
context.open(url) { _ in

}*/
}

let application =
UIApplication.value(forKeyPath: #keyPath(UIApplication.shared)) as! UIApplication
/* let webView = WKWebView()
webView.load(URLRequest(url: url))
*/
/*let application =
UIApplication.value(forKeyPath: #keyPath(UIApplication.shared)) as! UIApplication*/

if #available(iOS 10.0, *) {
/*if #available(iOS 10.0, *) {
application.open(url, options: [:], completionHandler: nil)
} else {
application.openURL(url)
}
}*/
}

let notificationCenter = CFNotificationCenterGetDarwinNotifyCenter()

func notifyAppWithName(name: String) {
let notificationName = CFNotificationName(name as CFString)

CFNotificationCenterPostNotification(notificationCenter, notificationName, nil, nil, false)
}

func sendNotification(contents: [String: Any], placeholders: [String: String?]) {
Expand Down
38 changes: 23 additions & 15 deletions src/ReactNativeDeviceActivity.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,11 +136,17 @@ export type DeviceActivityEvent = {
includesPastActivity?: boolean;
};

export type ShieldActionType = "unblockAll" | "dismiss" | "unblockCurrentApp";
export type ShieldActionType =
| "unblockAll"
| "dismiss"
| "unblockCurrentApp"
| "sendNotification"
| "openApp";

export type ShieldAction = {
type: ShieldActionType;
delay?: number;
payload?: NotificationPayload;
behavior: "close" | "defer";
};

Expand All @@ -149,6 +155,21 @@ export type ShieldActions = {
secondary?: ShieldAction;
};

export type NotificationPayload = {
title: string;
body: string;
sound?: "default" | "defaultCritical" | "defaultRingtone";
categoryIdentifier?: string;
badge?: number;
userInfo?: Record<string, any>;
interruptionLevel?: "active" | "critical" | "passive";
targetContentIdentifier?: string;
launchImageName?: string;
identifier?: string;
threadIdentifier?: string;
subtitle?: string;
};

export type Action =
| {
type: "blockSelection";
Expand All @@ -175,20 +196,7 @@ export type Action =
}
| {
type: "sendNotification";
payload: {
title: string;
body: string;
sound?: "default" | "defaultCritical" | "defaultRingtone";
categoryIdentifier?: string;
badge?: number;
userInfo?: Record<string, any>;
interruptionLevel?: "active" | "critical" | "passive";
targetContentIdentifier?: string;
launchImageName?: string;
identifier?: string;
threadIdentifier?: string;
subtitle?: string;
};
payload: NotificationPayload;
sleepBefore?: number;
sleepAfter?: number;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,6 @@ import os
// Make sure that your class name matches the NSExtensionPrincipalClass in your Info.plist.
@available(iOS 15.0, *)
class DeviceActivityMonitorExtension: DeviceActivityMonitor {
let notificationCenter = CFNotificationCenterGetDarwinNotifyCenter()
let store = ManagedSettingsStore()

func notifyAppWithName(name: String) {
let notificationName = CFNotificationName(name as CFString)

CFNotificationCenterPostNotification(notificationCenter, notificationName, nil, nil, false)
}

override func intervalDidStart(for activity: DeviceActivityName) {
super.intervalDidStart(for: activity)
logger.log("intervalDidStart")
Expand All @@ -33,7 +24,7 @@ class DeviceActivityMonitorExtension: DeviceActivityMonitor {
callbackName: "intervalDidStart"
)

self.notifyAppWithName(name: "intervalDidStart")
notifyAppWithName(name: "intervalDidStart")

self.executeActionsForEvent(activityName: activity.rawValue, callbackName: "intervalDidStart")
}
Expand All @@ -49,7 +40,7 @@ class DeviceActivityMonitorExtension: DeviceActivityMonitor {

CFPreferencesAppSynchronize(kCFPreferencesCurrentApplication)

self.notifyAppWithName(name: "intervalDidEnd")
notifyAppWithName(name: "intervalDidEnd")

self.executeActionsForEvent(activityName: activity.rawValue, callbackName: "intervalDidEnd")
}
Expand Down Expand Up @@ -84,7 +75,7 @@ class DeviceActivityMonitorExtension: DeviceActivityMonitor {
eventName: event.rawValue
)

self.notifyAppWithName(name: "eventDidReachThreshold")
notifyAppWithName(name: "eventDidReachThreshold")

self.executeActionsForEvent(
activityName: activity.rawValue, callbackName: "eventDidReachThreshold",
Expand All @@ -100,7 +91,7 @@ class DeviceActivityMonitorExtension: DeviceActivityMonitor {
callbackName: "intervalWillStartWarning"
)

self.notifyAppWithName(name: "intervalWillStartWarning")
notifyAppWithName(name: "intervalWillStartWarning")

self.executeActionsForEvent(
activityName: activity.rawValue, callbackName: "intervalWillStartWarning")
Expand All @@ -115,7 +106,7 @@ class DeviceActivityMonitorExtension: DeviceActivityMonitor {
callbackName: "intervalWillEndWarning"
)

self.notifyAppWithName(name: "intervalWillEndWarning")
notifyAppWithName(name: "intervalWillEndWarning")

self.executeActionsForEvent(
activityName: activity.rawValue, callbackName: "intervalWillEndWarning")
Expand All @@ -133,7 +124,7 @@ class DeviceActivityMonitorExtension: DeviceActivityMonitor {
eventName: event.rawValue
)

self.notifyAppWithName(name: "eventWillReachThresholdWarning")
notifyAppWithName(name: "eventWillReachThresholdWarning")

self.executeActionsForEvent(
activityName: activity.rawValue, callbackName: "eventWillReachThresholdWarning",
Expand Down
15 changes: 15 additions & 0 deletions targets/ShieldAction/ShieldActionExtension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,21 @@ func handleAction(
unblockAllApps()
}

if type == "sendNotification" {
// todo: replace with general string
/*DispatchQueue.main.async(execute: {
openUrl(urlString: "device-activity://")
})

notifyAppWithName(name: "fromShieldExtensions")

sleep(ms: 1)*/

if let payload = configForSelectedAction["payload"] as? [String: Any] {
sendNotification(contents: payload, placeholders: [:])
}
}

if type == "unblockCurrentApp" {
let unblockedSelectionStr = userDefaults?.string(forKey: "unblockedSelection")

Expand Down

0 comments on commit a6f70fe

Please sign in to comment.