diff --git a/ios/Plugin/DailyNotificationPlugin.swift b/ios/Plugin/DailyNotificationPlugin.swift index 4f5151d..96051e6 100644 --- a/ios/Plugin/DailyNotificationPlugin.swift +++ b/ios/Plugin/DailyNotificationPlugin.swift @@ -1012,6 +1012,125 @@ public class DailyNotificationPlugin: CAPPlugin { call.resolve() } + /** + * Check if channel is enabled + * + * iOS doesn't have notification channels like Android. + * This checks if notifications are enabled for the app. + * + * Equivalent to Android's isChannelEnabled method. + */ + @objc func isChannelEnabled(_ call: CAPPluginCall) { + let channelId = call.getString("channelId") ?? "daily_notification_channel" + + print("DNP-PLUGIN: Checking channel enabled: \(channelId)") + + notificationCenter.getNotificationSettings { settings in + let enabled = settings.authorizationStatus == .authorized + + let result: [String: Any] = [ + "enabled": enabled, + "channelId": channelId + ] + + DispatchQueue.main.async { + call.resolve(result) + } + } + } + + /** + * Open channel settings + * + * Opens iOS notification settings for the app. + * iOS doesn't have per-channel settings like Android. + * + * Equivalent to Android's openChannelSettings method. + */ + @objc func openChannelSettings(_ call: CAPPluginCall) { + let channelId = call.getString("channelId") ?? "daily_notification_channel" + + print("DNP-PLUGIN: Opening channel settings: \(channelId)") + + // iOS doesn't have per-channel settings + // Open app notification settings instead + if let settingsUrl = URL(string: UIApplication.openSettingsURLString) { + if UIApplication.shared.canOpenURL(settingsUrl) { + UIApplication.shared.open(settingsUrl) { success in + let result: [String: Any] = [ + "opened": success, + "channelId": channelId + ] + call.resolve(result) + } + } else { + let result: [String: Any] = [ + "opened": false, + "channelId": channelId, + "error": "Cannot open settings URL" + ] + call.resolve(result) + } + } else { + let result: [String: Any] = [ + "opened": false, + "channelId": channelId, + "error": "Invalid settings URL" + ] + call.resolve(result) + } + } + + /** + * Check status + * + * Comprehensive status check for notifications. + * Returns permission status, scheduling status, and other relevant information. + * + * Equivalent to Android's checkStatus method. + */ + @objc func checkStatus(_ call: CAPPluginCall) { + print("DNP-PLUGIN: Checking comprehensive status") + + // Get notification authorization status + notificationCenter.getNotificationSettings { settings in + let notificationsEnabled = settings.authorizationStatus == .authorized + + // Get notification status + self.notificationCenter.getPendingNotificationRequests { requests in + let dailyNotifications = requests.filter { $0.identifier.hasPrefix("daily_") } + + // Get schedules from UserDefaults + let schedules = self.getSchedulesFromUserDefaults() + let notifySchedules = schedules.filter { + ($0["kind"] as? String) == "notify" && + ($0["enabled"] as? Bool) == true + } + + // Calculate next notification time + var nextNotificationTime: TimeInterval = 0 + if let nextRunTimes = notifySchedules.compactMap({ $0["nextRunTime"] as? TimeInterval }) as? [TimeInterval], + !nextRunTimes.isEmpty { + nextNotificationTime = nextRunTimes.min() ?? 0 + } + + let result: [String: Any] = [ + "notificationsEnabled": notificationsEnabled, + "isScheduled": !notifySchedules.isEmpty, + "scheduledCount": notifySchedules.count, + "pendingCount": dailyNotifications.count, + "nextNotificationTime": nextNotificationTime, + "channelId": "daily_notification_channel", + "channelEnabled": notificationsEnabled + ] + + DispatchQueue.main.async { + call.resolve(result) + } + } + } + } + // MARK: - Private Implementation Methods private func setupBackgroundTasks() {