remove scheduleApiNotifications so the plugin cannot create new api_* schedules

This commit is contained in:
Jose Olarte III
2026-09-23 20:09:12 +08:00
parent 69b243c09b
commit cf110f69fb
7 changed files with 20 additions and 184 deletions
+6 -59
View File
@@ -32,7 +32,8 @@ public class DailyNotificationPlugin: CAPPlugin {
// Background task identifiers
private let fetchTaskIdentifier = "org.timesafari.dailynotification.fetch"
private let notifyTaskIdentifier = "org.timesafari.dailynotification.notify"
/// Prefix for API-managed batch notification identifiers: `api_\(epochMillis)`.
/// Prefix for leftover API-managed batch notification identifiers: `api_\(epochMillis)`.
/// Used by `clearApiNotifications` and Daily Reminder legacy-id cancellation; new `api_*` requests are not scheduled.
private let apiNotificationPrefix = "api_"
/// Prefix for Daily Reminder UNNotificationRequest identifiers: `daily_\(logicalReminderId)`.
private let dailyReminderNotificationPrefix = "daily_"
@@ -1123,9 +1124,9 @@ public class DailyNotificationPlugin: CAPPlugin {
notificationCenter.removePendingNotificationRequests(withIdentifiers: ids)
}
// MARK: - API batch notifications (replace-all UNUserNotificationCenter)
// MARK: - Leftover API batch notifications (clear-only; scheduling retired)
/// Removes only pending and delivered API-managed notifications (`api_*`). Does not touch Daily Reminders (`daily_*`), dual/org IDs, or other stacks.
/// Removes only pending and delivered leftover API-managed notifications (`api_*`). Does not touch Daily Reminders (`daily_*`), dual/org IDs, or other stacks.
@objc func clearApiNotifications(_ call: CAPPluginCall) {
NSLog("DNP-BATCH: clearApiNotifications — removing api_* pending and delivered only")
print("DNP-BATCH: clearApiNotifications — removing api_* pending and delivered only")
@@ -1156,59 +1157,6 @@ public class DailyNotificationPlugin: CAPPlugin {
}
}
/// Add one-shot reminders at epoch-ms timestamps. Does not remove other requests; identical IDs replace pending entries. Caller should clear first if needed.
/// Adds/overwrites API-managed notifications using deterministic IDs.
/// Does NOT clear existing notifications. Caller is responsible for lifecycle.
@objc func scheduleApiNotifications(_ call: CAPPluginCall) {
guard let timestamps = call.getArray("timestamps", Double.self) else {
call.reject("Missing timestamps")
return
}
NSLog("DNP-BATCH: scheduleApiNotifications — additive scheduling for \(timestamps.count) timestamp(s)")
print("DNP-BATCH: scheduleApiNotifications — additive scheduling for \(timestamps.count) timestamp(s)")
for ts in timestamps {
let date = Date(timeIntervalSince1970: ts / 1000)
guard date.timeIntervalSinceNow > 0 else {
NSLog("DNP-BATCH: skip stale timestamp ts=\(ts) (not in the future)")
print("DNP-BATCH: skip stale timestamp ts=\(ts) (not in the future)")
continue
}
let interval = date.timeIntervalSinceNow
let trigger = UNTimeIntervalNotificationTrigger(
timeInterval: interval,
repeats: false
)
let content = UNMutableNotificationContent()
content.title = "Reminder"
content.body = "You have a scheduled notification"
let id = "\(apiNotificationPrefix)\(Int(ts))"
NSLog("DNP-BATCH: scheduling ts=\(ts) interval=\(interval)s id=\(id)")
print("DNP-BATCH: scheduling ts=\(ts) interval=\(interval)s id=\(id)")
let request = UNNotificationRequest(
identifier: id,
content: content,
trigger: trigger
)
notificationCenter.add(request) { error in
if let error = error {
NSLog("DNP-BATCH: add failed id=\(id) error=\(error.localizedDescription)")
print("DNP-BATCH: add failed id=\(id) error=\(error.localizedDescription)")
}
}
}
call.resolve()
}
// MARK: - Static Daily Reminder Methods
@objc func scheduleDailyReminder(_ call: CAPPluginCall) {
@@ -1712,7 +1660,7 @@ public class DailyNotificationPlugin: CAPPlugin {
let fireDate = Date().addingTimeInterval(TimeInterval(validSeconds))
let epochMillis = Int64(fireDate.timeIntervalSince1970 * 1000)
let requestId = apiNotificationId(epochMillis: epochMillis)
let requestId = "test_\(epochMillis)"
let request = UNNotificationRequest(
identifier: requestId,
content: notificationContent,
@@ -2803,9 +2751,8 @@ extension DailyNotificationPlugin {
methods.append(CAPPluginMethod(name: "getScheduledReminders", returnType: CAPPluginReturnPromise))
methods.append(CAPPluginMethod(name: "updateDailyReminder", returnType: CAPPluginReturnPromise))
// API batch notifications (replace-all UNUserNotificationCenter)
// Leftover API batch notifications (clear-only; scheduling retired)
methods.append(CAPPluginMethod(name: "clearApiNotifications", returnType: CAPPluginReturnPromise))
methods.append(CAPPluginMethod(name: "scheduleApiNotifications", returnType: CAPPluginReturnPromise))
// Dual scheduling methods
methods.append(CAPPluginMethod(name: "scheduleContentFetch", returnType: CAPPluginReturnPromise))