Reputation: 59
I use the AlarmManager to send an Alarm and an BroadcastReceiver to get this Alarm. My App also uses a Room Database with an NotificationDao.
I need to add something with "upsertNotification" to the Database when the Alarm is received. How can I archive this?
BroadcastReceiver:
class AlarmReceiver : BroadcastReceiver() {
override fun onReceive(context: Context?, intent: Intent?) {
val message = intent?.getStringExtra("EXTRA_MESSAGE") ?: return
val notificationType =
NotificationType.fromValue(intent.getIntExtra("NOTIFICATION_TYPE", 0))
val notification = Notification(
id = null,
date = LocalDate.now(),
time = LocalTime.now(),
title = "Test Title Premium",
description = message,
unread = true,
notificationType = NotificationType.PremiumAdvertising.value
)
context?.let { DayReminderNotificationService(it) }?.showNotification(message)
}
}
NotificationDao:
@Dao
interface NotificationDao {
@Upsert
suspend fun upsertNotification(notification: Notification)
@Delete
suspend fun deleteNotification(notification: Notification)
@Query("SELECT * FROM notifications")
fun getAllNotifications(): Flow<List<Notification>>
@Query("SELECT * FROM notifications WHERE unread = 1")
fun getAllUnreadNotifications(): Flow<List<Notification>>
}
@Module
@InstallIn(SingletonComponent::class)
object CoreModule {
@Provides
@Singleton
fun provideNotificationDao(database: AppDatabase) = database.notificationDao()
}
AlarmScheduler:
class AndroidAlarmSchedulerImpl(
private val context: Context
) : AlarmScheduler {
private val alarmManager = context.getSystemService(AlarmManager::class.java)
override fun schedule(item: AlarmItem) {
val intent = Intent(context, AlarmReceiver::class.java).apply {
putExtra("EXTRA_MESSAGE", item.message)
putExtra("NOTIFICATION_TYPE", item.notificationType)
}
alarmManager.setRepeating(
AlarmManager.RTC_WAKEUP,
item.time.atZone(ZoneId.systemDefault()).toEpochSecond() * 1000,
60000,
PendingIntent.getBroadcast(
context,
item.hashCode(),
intent,
PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE
)
)
}
override fun cancel(item: AlarmItem) {
alarmManager.cancel(
PendingIntent.getBroadcast(
context,
item.hashCode(),
Intent(context, AlarmReceiver::class.java),
PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE
)
)
}
}
Upvotes: 0
Views: 34
Reputation: 59
I now found a solution. I don't know if it is the right way, but it works.
@Module
@InstallIn(SingletonComponent::class)
object AlarmModule {
@Provides
@Singleton
fun provideAddDayReminderNotificationSingleton(
notificationUseCases: NotificationUseCases,
dayUseCases: DayUseCases
): AddDayReminderNotification {
return AddDayReminderNotification(notificationUseCases, dayUseCases)
}
}
@AndroidEntryPoint
class AlarmReceiver : BroadcastReceiver() {
@Inject
lateinit var addDayReminderNotificationSingleton: AddDayReminderNotification
...
}
@Singleton
class AddDayReminderNotification(
private val notificationUseCases: NotificationUseCases,
private val dayUseCases: DayUseCases
) {
...
}
Upvotes: 0