Kotlin BroadcastReceiver Room Dao

问题描述 投票:0回答:1

我使用 AlarmManager 发送警报,并使用 BroadcastReceiver 来获取该警报。 我的应用程序还使用带有NotificationDao的房间数据库。

收到警报时,我需要将带有“upsertNotification”的内容添加到数据库中。 我怎样才能存档这个?

广播接收器:

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()
}

闹钟调度程序:

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
            )
        )
    }
}
android kotlin notifications android-room alarmmanager
1个回答
0
投票

我现在找到了解决方案。我不知道这是否是正确的方法,但它确实有效。

@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
) {
...
}
© www.soinside.com 2019 - 2024. All rights reserved.