diff --git a/ios/Plugin/DailyNotificationPlugin.swift b/ios/Plugin/DailyNotificationPlugin.swift index 464f057..2ef91d5 100644 --- a/ios/Plugin/DailyNotificationPlugin.swift +++ b/ios/Plugin/DailyNotificationPlugin.swift @@ -839,6 +839,89 @@ public class DailyNotificationPlugin: CAPPlugin { call.resolve() } + // MARK: - Content Management Methods + + /** + * Get content cache + * + * Returns the latest cached content from Core Data. + * + * Equivalent to Android's getContentCache method. + */ + @objc func getContentCache(_ call: CAPPluginCall) { + print("DNP-PLUGIN: Getting content cache") + + Task { + do { + let context = persistenceController.container.viewContext + let request: NSFetchRequest = ContentCache.fetchRequest() + request.sortDescriptors = [NSSortDescriptor(keyPath: \ContentCache.fetchedAt, ascending: false)] + request.fetchLimit = 1 + + let results = try context.fetch(request) + + if let latest = results.first { + let payload = try JSONSerialization.jsonObject(with: latest.payload!) as? [String: Any] ?? [:] + + let result: [String: Any] = [ + "id": latest.id ?? "", + "fetchedAt": Int64((latest.fetchedAt?.timeIntervalSince1970 ?? 0) * 1000), + "ttlSeconds": latest.ttlSeconds, + "payload": payload, + "meta": latest.meta ?? "" + ] + + DispatchQueue.main.async { + call.resolve(result) + } + } else { + // Return empty result if no cache + DispatchQueue.main.async { + call.resolve([:]) + } + } + } catch { + print("DNP-PLUGIN: Failed to get content cache: \(error)") + DispatchQueue.main.async { + call.reject("Content cache retrieval failed: \(error.localizedDescription)") + } + } + } + } + + /** + * Clear content cache + * + * Clears all content cache entries from Core Data. + * + * Equivalent to Android's clearContentCache method. + */ + @objc func clearContentCache(_ call: CAPPluginCall) { + print("DNP-PLUGIN: Clearing content cache") + + Task { + do { + let context = persistenceController.container.viewContext + let request: NSFetchRequest = ContentCache.fetchRequest() + let deleteRequest = NSBatchDeleteRequest(fetchRequest: request) + + try context.execute(deleteRequest) + try context.save() + + print("DNP-PLUGIN: Content cache cleared successfully") + + DispatchQueue.main.async { + call.resolve() + } + } catch { + print("DNP-PLUGIN: Failed to clear content cache: \(error)") + DispatchQueue.main.async { + call.reject("Failed to clear content cache: \(error.localizedDescription)") + } + } + } + } + // MARK: - Private Implementation Methods private func setupBackgroundTasks() {