diff --git a/ios/Plugin/DailyNotificationPlugin.swift b/ios/Plugin/DailyNotificationPlugin.swift index 6e4744b..464f057 100644 --- a/ios/Plugin/DailyNotificationPlugin.swift +++ b/ios/Plugin/DailyNotificationPlugin.swift @@ -735,6 +735,110 @@ public class DailyNotificationPlugin: CAPPlugin { } } + /** + * Configure native fetcher + * + * Configures the native content fetcher with API credentials. + * Stores configuration in UserDefaults for persistence. + * + * Note: iOS doesn't have the same native fetcher interface as Android. + * Configuration is stored and can be used by background fetch tasks. + * + * Equivalent to Android's configureNativeFetcher method. + */ + @objc func configureNativeFetcher(_ call: CAPPluginCall) { + guard let options = call.options else { + call.reject("Options are required") + return + } + + // Extract required parameters + guard let apiBaseUrl = options["apiBaseUrl"] as? String else { + call.reject("apiBaseUrl is required") + return + } + + guard let activeDid = options["activeDid"] as? String else { + call.reject("activeDid is required") + return + } + + // Support both jwtToken and jwtSecret for backward compatibility + guard let jwtToken = (options["jwtToken"] as? String) ?? (options["jwtSecret"] as? String) else { + call.reject("jwtToken or jwtSecret is required") + return + } + + print("DNP-PLUGIN: Configuring native fetcher: apiBaseUrl=\(apiBaseUrl), activeDid=\(activeDid.prefix(30))...") + + // Store configuration in UserDefaults (matching Android database storage) + let configId = "native_fetcher_config" + let configKey = "DailyNotificationNativeFetcherConfig" + + let config: [String: Any] = [ + "apiBaseUrl": apiBaseUrl, + "activeDid": activeDid, + "jwtToken": jwtToken, + "configuredAt": Date().timeIntervalSince1970 * 1000 + ] + + // Store as JSON string for consistency with Android + do { + let jsonData = try JSONSerialization.data(withJSONObject: config, options: []) + let jsonString = String(data: jsonData, encoding: .utf8) ?? "{}" + + UserDefaults.standard.set(jsonString, forKey: configKey) + UserDefaults.standard.synchronize() + + print("DNP-PLUGIN: Native fetcher configuration stored successfully") + call.resolve() + + } catch { + print("DNP-PLUGIN: Failed to store native fetcher config: \(error)") + call.reject("Failed to store configuration: \(error.localizedDescription)") + } + } + + /** + * Set active DID from host + * + * Sets the active DID (identity) for TimeSafari integration. + * This is a simpler method than configureNativeFetcher for just updating the DID. + * + * Equivalent to Android's setActiveDidFromHost method. + */ + @objc func setActiveDidFromHost(_ call: CAPPluginCall) { + guard let activeDid = call.getString("activeDid") else { + call.reject("activeDid is required") + return + } + + print("DNP-PLUGIN: Setting activeDid from host: \(activeDid.prefix(30))...") + + // Store activeDid in UserDefaults + let keyActiveDid = "DailyNotificationActiveDid" + UserDefaults.standard.set(activeDid, forKey: keyActiveDid) + UserDefaults.standard.synchronize() + + // If there's existing native fetcher config, update it + let configKey = "DailyNotificationNativeFetcherConfig" + if let configJson = UserDefaults.standard.string(forKey: configKey), + let configData = configJson.data(using: .utf8), + var config = try? JSONSerialization.jsonObject(with: configData) as? [String: Any] { + config["activeDid"] = activeDid + config["updatedAt"] = Date().timeIntervalSince1970 * 1000 + + if let updatedJsonData = try? JSONSerialization.data(withJSONObject: config, options: []), + let updatedJsonString = String(data: updatedJsonData, encoding: .utf8) { + UserDefaults.standard.set(updatedJsonString, forKey: configKey) + UserDefaults.standard.synchronize() + } + } + + print("DNP-PLUGIN: ActiveDid set successfully") + call.resolve() + } + // MARK: - Private Implementation Methods private func setupBackgroundTasks() {