From ca40b971c5e52ead141cdf892416a95d803b28bf Mon Sep 17 00:00:00 2001 From: Matthew Raymer Date: Tue, 11 Nov 2025 02:10:39 -0800 Subject: [PATCH] feat(ios): implement getPowerState, requestBatteryOptimizationExemption, and setAdaptiveScheduling methods Implemented power and scheduling utility methods: getPowerState(): - Returns power state code (0=unknown, 1=unplugged, 2=charging, 3=full) - Returns isOptimizationExempt (always false on iOS) - Uses UIDevice battery monitoring requestBatteryOptimizationExemption(): - No-op on iOS (battery optimization not applicable) - Exists for API compatibility with Android - Background App Refresh is user-controlled in Settings setAdaptiveScheduling(): - Enables/disables adaptive scheduling - Stores setting in UserDefaults - Matches Android behavior iOS Adaptations: - Battery optimization not applicable (Background App Refresh is system setting) - Power state derived from battery state - Adaptive scheduling stored in UserDefaults Progress: 24/52 methods implemented (46% complete) --- ios/Plugin/DailyNotificationPlugin.swift | 90 ++++++++++++++++++++++++ 1 file changed, 90 insertions(+) diff --git a/ios/Plugin/DailyNotificationPlugin.swift b/ios/Plugin/DailyNotificationPlugin.swift index 2ef91d5..4f5151d 100644 --- a/ios/Plugin/DailyNotificationPlugin.swift +++ b/ios/Plugin/DailyNotificationPlugin.swift @@ -922,6 +922,96 @@ public class DailyNotificationPlugin: CAPPlugin { } } + /** + * Get power state + * + * Returns power state information. + * + * Equivalent to Android's getPowerState method. + */ + @objc func getPowerState(_ call: CAPPluginCall) { + print("DNP-PLUGIN: Getting power state") + + // iOS doesn't have battery optimization like Android + // Background App Refresh is the closest equivalent, but we can't check it directly + let isOptimizationExempt = false + + // Get battery state for power state code + UIDevice.current.isBatteryMonitoringEnabled = true + let batteryState = UIDevice.current.batteryState + + // Map battery state to power state code (same as getBatteryStatus) + let powerState: Int + switch batteryState { + case .unknown: + powerState = 0 + case .unplugged: + powerState = 1 + case .charging: + powerState = 2 + case .full: + powerState = 3 + @unknown default: + powerState = 0 + } + + let result: [String: Any] = [ + "powerState": powerState, + "isOptimizationExempt": isOptimizationExempt + ] + + print("DNP-PLUGIN: Power state: \(powerState), optimizationExempt=\(isOptimizationExempt)") + + call.resolve(result) + } + + /** + * Request battery optimization exemption + * + * On iOS, this is a no-op as iOS doesn't have battery optimization settings + * like Android. Background App Refresh is controlled by the user in Settings. + * + * Equivalent to Android's requestBatteryOptimizationExemption method. + */ + @objc func requestBatteryOptimizationExemption(_ call: CAPPluginCall) { + print("DNP-PLUGIN: Requesting battery optimization exemption (iOS: no-op)") + + // iOS doesn't have battery optimization exemption like Android + // Background App Refresh is a system setting that users control + // We can't programmatically request exemption on iOS + // This method exists for API compatibility but does nothing + + call.resolve() + } + + /** + * Set adaptive scheduling + * + * Enables or disables adaptive scheduling features. + * + * Equivalent to Android's setAdaptiveScheduling method. + */ + @objc func setAdaptiveScheduling(_ call: CAPPluginCall) { + guard let options = call.options else { + call.reject("Options are required") + return + } + + guard let enabled = options["enabled"] as? Bool else { + call.reject("enabled boolean is required") + return + } + + print("DNP-PLUGIN: Setting adaptive scheduling: enabled=\(enabled)") + + // Store adaptive scheduling setting in UserDefaults + UserDefaults.standard.set(enabled, forKey: "DailyNotificationAdaptiveScheduling") + UserDefaults.standard.synchronize() + + print("DNP-PLUGIN: Adaptive scheduling set successfully") + call.resolve() + } + // MARK: - Private Implementation Methods private func setupBackgroundTasks() {