diff --git a/android/src/main/java/com/timesafari/dailynotification/DailyNotificationPlugin.kt b/android/src/main/java/com/timesafari/dailynotification/DailyNotificationPlugin.kt index 8a88543..849c84a 100644 --- a/android/src/main/java/com/timesafari/dailynotification/DailyNotificationPlugin.kt +++ b/android/src/main/java/com/timesafari/dailynotification/DailyNotificationPlugin.kt @@ -1864,6 +1864,57 @@ open class DailyNotificationPlugin : Plugin() { } } + /** + * Get all schedules with their AlarmManager status + * Returns schedules from database with isActuallyScheduled flag for each + */ + @PluginMethod + fun getSchedulesWithStatus(call: PluginCall) { + CoroutineScope(Dispatchers.IO).launch { + try { + val options = call.getObject("options") + val kind = options?.getString("kind") + val enabled = options?.getBoolean("enabled") + + val context = context ?: return@launch call.reject("Context not available") + + val schedules = when { + kind != null && enabled != null -> + getDatabase().scheduleDao().getByKindAndEnabled(kind, enabled) + kind != null -> + getDatabase().scheduleDao().getByKind(kind) + enabled != null -> + if (enabled) getDatabase().scheduleDao().getEnabled() else getDatabase().scheduleDao().getAll().filter { !it.enabled } + else -> + getDatabase().scheduleDao().getAll() + } + + // For each schedule, check if it's actually scheduled in AlarmManager + val schedulesArray = org.json.JSONArray() + schedules.forEach { schedule -> + val scheduleJson = scheduleToJson(schedule) + + // Only check AlarmManager status for "notify" schedules with nextRunAt + if (schedule.kind == "notify" && schedule.nextRunAt != null) { + val isScheduled = NotifyReceiver.isAlarmScheduled(context, schedule.nextRunAt!!) + scheduleJson.put("isActuallyScheduled", isScheduled) + } else { + scheduleJson.put("isActuallyScheduled", false) + } + + schedulesArray.put(scheduleJson) + } + + call.resolve(JSObject().apply { + put("schedules", schedulesArray) + }) + } catch (e: Exception) { + Log.e(TAG, "Failed to get schedules with status", e) + call.reject("Failed to get schedules with status: ${e.message}") + } + } + } + @PluginMethod fun createSchedule(call: PluginCall) { CoroutineScope(Dispatchers.IO).launch { diff --git a/src/definitions.ts b/src/definitions.ts index 4b16c71..87106ed 100644 --- a/src/definitions.ts +++ b/src/definitions.ts @@ -328,6 +328,15 @@ export interface Schedule { stateJson?: string; } +/** + * Schedule with AlarmManager status + * Extends Schedule with isActuallyScheduled flag indicating if alarm is registered in AlarmManager + */ +export interface ScheduleWithStatus extends Schedule { + /** Whether the alarm is actually scheduled in AlarmManager (Android only, for 'notify' schedules) */ + isActuallyScheduled: boolean; +} + /** * Input type for creating a new schedule */ @@ -697,6 +706,29 @@ export interface DailyNotificationPlugin { */ getSchedules(options?: { kind?: 'fetch' | 'notify'; enabled?: boolean }): Promise<{ schedules: Schedule[] }>; + /** + * Get all schedules with their AlarmManager status + * Returns schedules from database with isActuallyScheduled flag for each + * + * @param options Optional filters: + * - kind: Filter by schedule type ('fetch' | 'notify') + * - enabled: Filter by enabled status (true = only enabled, false = only disabled, undefined = all) + * @returns Promise resolving to object with schedules array: { schedules: ScheduleWithStatus[] } + * + * @example + * ```typescript + * // Get all notification schedules with AlarmManager status + * const result = await DailyNotification.getSchedulesWithStatus({ + * kind: 'notify', + * enabled: true + * }); + * result.schedules.forEach(schedule => { + * console.log(`${schedule.id}: ${schedule.isActuallyScheduled ? 'Scheduled' : 'Not scheduled'}`); + * }); + * ``` + */ + getSchedulesWithStatus(options?: { kind?: 'fetch' | 'notify'; enabled?: boolean }): Promise<{ schedules: ScheduleWithStatus[] }>; + /** * Get a single schedule by ID * diff --git a/test-apps/android-test-app/app/src/main/assets/public/index.html b/test-apps/android-test-app/app/src/main/assets/public/index.html index 5f60ee0..618650a 100644 --- a/test-apps/android-test-app/app/src/main/assets/public/index.html +++ b/test-apps/android-test-app/app/src/main/assets/public/index.html @@ -74,6 +74,14 @@ + + +