From 928733f87fb140ce6afd022fcd475675bfb55ae6 Mon Sep 17 00:00:00 2001 From: Matthew Raymer Date: Tue, 11 Nov 2025 01:56:42 -0800 Subject: [PATCH] feat(ios): implement getBatteryStatus method Implemented battery status method matching Android functionality: getBatteryStatus(): - Gets battery level (0-100%) using UIDevice - Detects charging state (charging or full) - Maps battery state to power state code (0=unknown, 1=unplugged, 2=charging, 3=full) - Returns isOptimizationExempt (always false on iOS - no battery optimization) - Enables battery monitoring automatically iOS Adaptations: - Uses UIDevice.current for battery information - Battery optimization not applicable on iOS (Background App Refresh is system setting) - Returns -1 for battery level if unknown Progress: 16/52 methods implemented (31% complete) --- ios/Plugin/DailyNotificationPlugin.swift | 59 ++++++++++++++++++++++++ 1 file changed, 59 insertions(+) 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() {