diff --git a/android/plugin/src/main/java/com/timesafari/dailynotification/DailyNotificationWorker.java b/android/plugin/src/main/java/com/timesafari/dailynotification/DailyNotificationWorker.java index aa29747..6151c96 100644 --- a/android/plugin/src/main/java/com/timesafari/dailynotification/DailyNotificationWorker.java +++ b/android/plugin/src/main/java/com/timesafari/dailynotification/DailyNotificationWorker.java @@ -32,6 +32,7 @@ import java.util.concurrent.ConcurrentHashMap; import com.timesafari.dailynotification.storage.DailyNotificationStorageRoom; import com.timesafari.dailynotification.entities.NotificationContentEntity; +import com.timesafari.dailynotification.DailyNotificationFetcher; /** * WorkManager worker for processing daily notifications @@ -512,6 +513,36 @@ public class DailyNotificationWorker extends Worker { // Log next scheduled time in readable format String nextTimeStr = formatScheduledTime(nextScheduledTime); Log.i(TAG, "DN|RESCHEDULE_OK id=" + content.getId() + " next=" + nextTimeStr); + + // Schedule background fetch for next notification (5 minutes before scheduled time) + try { + DailyNotificationStorage storageForFetcher = new DailyNotificationStorage(getApplicationContext()); + DailyNotificationStorageRoom roomStorageForFetcher = new DailyNotificationStorageRoom(getApplicationContext()); + DailyNotificationFetcher fetcher = new DailyNotificationFetcher( + getApplicationContext(), + storageForFetcher, + roomStorageForFetcher + ); + + // Calculate fetch time (5 minutes before notification) + long fetchTime = nextScheduledTime - TimeUnit.MINUTES.toMillis(5); + long currentTime = System.currentTimeMillis(); + + if (fetchTime > currentTime) { + fetcher.scheduleFetch(fetchTime); + Log.i(TAG, "DN|RESCHEDULE_PREFETCH_SCHEDULED id=" + content.getId() + + " next_fetch=" + fetchTime + + " next_notification=" + nextScheduledTime); + } else { + Log.w(TAG, "DN|RESCHEDULE_PREFETCH_PAST id=" + content.getId() + + " fetch_time=" + fetchTime + + " current=" + currentTime); + fetcher.scheduleImmediateFetch(); + } + } catch (Exception e) { + Log.e(TAG, "DN|RESCHEDULE_PREFETCH_ERR id=" + content.getId() + + " error scheduling prefetch", e); + } } else { Log.e(TAG, "DN|RESCHEDULE_ERR id=" + content.getId()); }