From d27e5c57a2b5a278afeb7bf51618e201e194ce8a Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Thu, 26 Feb 2026 02:35:23 +0000 Subject: [PATCH] Refactor NotificationService to use WorkManager for scheduling Replaced unreliable coroutine-based scheduling in `scheduleMorningBriefing` with `WorkManager` via `WorkScheduler`. Updated `sendDailyBriefing` to accept optional weather parameters for dynamic notification content. Added overloads for `sendRainAlert`, `sendSevereWeatherAlert`, and `sendUVWarning` to align with `WeatherWorker` usage. Removed unused coroutine imports and scope. Co-authored-by: singhaditya21 <53948039+singhaditya21@users.noreply.github.com> --- .../app/service/NotificationService.kt | 80 +++++++++++++------ 1 file changed, 54 insertions(+), 26 deletions(-) diff --git a/android/app/src/main/kotlin/com/climaai/app/service/NotificationService.kt b/android/app/src/main/kotlin/com/climaai/app/service/NotificationService.kt index 3652f02..678dc0d 100644 --- a/android/app/src/main/kotlin/com/climaai/app/service/NotificationService.kt +++ b/android/app/src/main/kotlin/com/climaai/app/service/NotificationService.kt @@ -13,10 +13,7 @@ import androidx.core.app.NotificationCompat import androidx.core.app.NotificationManagerCompat import com.climaai.app.MainActivity import com.climaai.app.R -import kotlinx.coroutines.CoroutineScope -import kotlinx.coroutines.Dispatchers -import kotlinx.coroutines.delay -import kotlinx.coroutines.launch +import com.climaai.app.work.WorkScheduler import java.util.Calendar /** @@ -60,7 +57,6 @@ enum class NotificationType( class NotificationService(private val context: Context) { private val notificationManager = NotificationManagerCompat.from(context) - private val scope = CoroutineScope(Dispatchers.IO) /** * Initialize notification channels @@ -115,6 +111,19 @@ class NotificationService(private val context: Context) { showNotification(NotificationType.WEATHER_ALERT.ordinal * 100 + 1, notification) } + /** + * Send severe weather alert (Overload for WeatherWorker) + */ + fun sendSevereWeatherAlert( + alertType: String, + severity: String, + description: String, + actionRequired: String + ) { + val message = "$description. $actionRequired." + sendSevereWeatherAlert(title = alertType, message = message, severity = severity) + } + /** * Send rain alert */ @@ -142,6 +151,23 @@ class NotificationService(private val context: Context) { showNotification(NotificationType.RAIN_ALERT.ordinal * 100 + 1, notification) } + /** + * Send rain alert (Overload for WeatherWorker) + */ + fun sendRainAlert(precipitationChance: Int, estimatedTime: String) { + val title = "🌧️ Rain Alert" + val message = "Rain is expected $estimatedTime with a $precipitationChance% chance." + + val notification = createNotification( + channelId = NotificationType.RAIN_ALERT.channelId, + title = title, + message = message, + priority = NotificationCompat.PRIORITY_HIGH + ) + + showNotification(NotificationType.RAIN_ALERT.ordinal * 100 + 1, notification) + } + /** * Send UV warning */ @@ -172,6 +198,14 @@ class NotificationService(private val context: Context) { showNotification(NotificationType.UV_WARNING.ordinal * 100 + 1, notification) } + /** + * Send UV warning (Overload for WeatherWorker) + */ + fun sendUVWarning(uvIndex: Int, riskLevel: String) { + // Reuse the logic from the main method + sendUVWarning(uvIndex) + } + /** * Send pollen alert */ @@ -198,35 +232,29 @@ class NotificationService(private val context: Context) { * Schedule daily morning briefing */ fun scheduleMorningBriefing(hour: Int = 7, minute: Int = 0) { - // In production, use WorkManager for reliable scheduling - scope.launch { - val now = Calendar.getInstance() - val scheduledTime = Calendar.getInstance().apply { - set(Calendar.HOUR_OF_DAY, hour) - set(Calendar.MINUTE, minute) - set(Calendar.SECOND, 0) - - // If time has passed today, schedule for tomorrow - if (before(now)) { - add(Calendar.DAY_OF_MONTH, 1) - } - } - - val delayMs = scheduledTime.timeInMillis - now.timeInMillis - delay(delayMs) - - sendDailyBriefing() - } + // Use WorkManager for reliable scheduling + WorkScheduler.scheduleDailySummary(context, hour) } /** * Send daily briefing notification */ - fun sendDailyBriefing() { + fun sendDailyBriefing( + summary: String? = null, + highTemp: Int? = null, + lowTemp: Int? = null, + precipChance: Int? = null + ) { + val message = if (summary != null && highTemp != null && lowTemp != null) { + "Today: $summary. High: $highTemp°F, Low: $lowTemp°F. ${if (precipChance != null && precipChance > 20) "Chance of rain: $precipChance%." else "Enjoy your day!"}" + } else { + "Today's forecast: Partly cloudy, high of 72°F. Perfect day for outdoor activities!" + } + val notification = createNotification( channelId = NotificationType.DAILY_BRIEFING.channelId, title = "☀️ Good Morning!", - message = "Today's forecast: Partly cloudy, high of 72°F. Perfect day for outdoor activities!", + message = message, priority = NotificationCompat.PRIORITY_DEFAULT )