diff --git a/ios/Plugin/DailyNotificationPlugin.swift b/ios/Plugin/DailyNotificationPlugin.swift index cd39922..d49918c 100644 --- a/ios/Plugin/DailyNotificationPlugin.swift +++ b/ios/Plugin/DailyNotificationPlugin.swift @@ -11,6 +11,7 @@ import Capacitor import UserNotifications import BackgroundTasks import CoreData +import UIKit /** * iOS implementation of Daily Notification Plugin @@ -608,6 +609,64 @@ public class DailyNotificationPlugin: CAPPlugin { requestNotificationPermissions(call) } + /** + * Get battery status + * + * Returns battery information including: + * - level: Battery level (0-100) + * - isCharging: Whether device is charging + * - powerState: Power state code + * - isOptimizationExempt: Whether app is exempt from battery optimization (iOS: always false) + * + * Equivalent to Android's getBatteryStatus method. + */ + @objc func getBatteryStatus(_ call: CAPPluginCall) { + print("DNP-PLUGIN: Getting battery status") + + // Enable battery monitoring if not already enabled + UIDevice.current.isBatteryMonitoringEnabled = true + + // Get battery level (0.0 to 1.0, -1.0 if unknown) + let batteryLevel = UIDevice.current.batteryLevel + let batteryLevelPercent = batteryLevel >= 0 ? Int(batteryLevel * 100) : -1 + + // Get battery state + let batteryState = UIDevice.current.batteryState + let isCharging = batteryState == .charging || batteryState == .full + + // Map battery state to power state code + // 0 = unknown, 1 = unplugged, 2 = charging, 3 = full + 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 + } + + // iOS doesn't have battery optimization like Android + // Background App Refresh is the closest equivalent, but we can't check it directly + // Return false to indicate we're subject to system power management + let isOptimizationExempt = false + + let result: [String: Any] = [ + "level": batteryLevelPercent, + "isCharging": isCharging, + "powerState": powerState, + "isOptimizationExempt": isOptimizationExempt + ] + + print("DNP-PLUGIN: Battery status: level=\(batteryLevelPercent)%, charging=\(isCharging), state=\(powerState)") + + call.resolve(result) + } + // MARK: - Private Implementation Methods private func setupBackgroundTasks() {