fix: notification check on app launch

This commit is contained in:
rebelonion 2024-03-22 22:34:21 -05:00
parent dca6ffdbbe
commit a189802061
7 changed files with 50 additions and 4 deletions

View file

@ -121,7 +121,9 @@ class App : MultiDexApplication() {
}
val useAlarmManager = PrefManager.getVal<Boolean>(PrefName.UseAlarmManager)
TaskScheduler.create(this, useAlarmManager).scheduleAllTasks(this)
val scheduler = TaskScheduler.create(this, useAlarmManager)
scheduler.scheduleAllTasks(this)
scheduler.scheduleSingleWork(this)
}
private fun setupNotificationChannels() {

View file

@ -15,7 +15,7 @@ import java.util.concurrent.TimeUnit
class AlarmManagerScheduler(private val context: Context) : TaskScheduler {
override fun scheduleRepeatingTask(taskType: TaskType, interval: Long) {
if (interval < TimeUnit.MINUTES.toMillis(15)) {
if (interval * 1000 < TimeUnit.MINUTES.toMillis(15)) {
cancelTask(taskType)
return
}

View file

@ -1,6 +1,7 @@
package ani.dantotsu.notifications
import android.content.Context
import androidx.work.OutOfQuotaPolicy
import ani.dantotsu.notifications.anilist.AnilistNotificationWorker
import ani.dantotsu.notifications.comment.CommentNotificationWorker
import ani.dantotsu.notifications.subscription.SubscriptionNotificationWorker
@ -31,6 +32,31 @@ interface TaskScheduler {
}
}
fun scheduleSingleWork(context: Context) {
val workManager = androidx.work.WorkManager.getInstance(context)
workManager.enqueueUniqueWork(
CommentNotificationWorker.WORK_NAME,
androidx.work.ExistingWorkPolicy.REPLACE,
androidx.work.OneTimeWorkRequest.Builder(CommentNotificationWorker::class.java)
.setExpedited(OutOfQuotaPolicy.RUN_AS_NON_EXPEDITED_WORK_REQUEST)
.build()
)
workManager.enqueueUniqueWork(
AnilistNotificationWorker.WORK_NAME,
androidx.work.ExistingWorkPolicy.REPLACE,
androidx.work.OneTimeWorkRequest.Builder(AnilistNotificationWorker::class.java)
.setExpedited(OutOfQuotaPolicy.RUN_AS_NON_EXPEDITED_WORK_REQUEST)
.build()
)
workManager.enqueueUniqueWork(
SubscriptionNotificationWorker.WORK_NAME,
androidx.work.ExistingWorkPolicy.REPLACE,
androidx.work.OneTimeWorkRequest.Builder(SubscriptionNotificationWorker::class.java)
.setExpedited(OutOfQuotaPolicy.RUN_AS_NON_EXPEDITED_WORK_REQUEST)
.build()
)
}
companion object {
fun create(context: Context, useAlarmManager: Boolean): TaskScheduler {
return if (useAlarmManager) {

View file

@ -10,7 +10,7 @@ import ani.dantotsu.notifications.subscription.SubscriptionNotificationWorker
class WorkManagerScheduler(private val context: Context) : TaskScheduler {
override fun scheduleRepeatingTask(taskType: TaskType, interval: Long) {
if (interval < PeriodicWorkRequest.MIN_PERIODIC_INTERVAL_MILLIS) {
if (interval * 1000 < PeriodicWorkRequest.MIN_PERIODIC_INTERVAL_MILLIS) {
cancelTask(taskType)
return
}

View file

@ -10,6 +10,11 @@ class AnilistNotificationWorker(appContext: Context, workerParams: WorkerParamet
override suspend fun doWork(): Result {
Logger.log("AnilistNotificationWorker: doWork")
if (System.currentTimeMillis() - lastCheck < 60000) {
Logger.log("AnilistNotificationWorker: doWork skipped")
return Result.success()
}
lastCheck = System.currentTimeMillis()
return if (AnilistNotificationTask().execute(applicationContext)) {
Result.success()
} else {
@ -21,5 +26,6 @@ class AnilistNotificationWorker(appContext: Context, workerParams: WorkerParamet
companion object {
val checkIntervals = arrayOf(0L, 30, 60, 120, 240, 360, 720, 1440)
const val WORK_NAME = "ani.dantotsu.notifications.anilist.AnilistNotificationWorker"
private var lastCheck = 0L
}
}

View file

@ -11,6 +11,11 @@ class CommentNotificationWorker(appContext: Context, workerParams: WorkerParamet
CoroutineWorker(appContext, workerParams) {
override suspend fun doWork(): Result {
Logger.log("CommentNotificationWorker: doWork")
if (System.currentTimeMillis() - lastCheck < 60000) {
Logger.log("CommentNotificationWorker: doWork skipped")
return Result.success()
}
lastCheck = System.currentTimeMillis()
return if (CommentNotificationTask().execute(applicationContext)) {
Result.success()
} else {
@ -30,5 +35,6 @@ class CommentNotificationWorker(appContext: Context, workerParams: WorkerParamet
companion object {
val checkIntervals = arrayOf(0L, 480, 720, 1440)
const val WORK_NAME = "ani.dantotsu.notifications.comment.CommentNotificationWorker"
private var lastCheck = 0L
}
}

View file

@ -11,7 +11,12 @@ class SubscriptionNotificationWorker(appContext: Context, workerParams: WorkerPa
override suspend fun doWork(): Result {
Logger.log("SubscriptionNotificationWorker: doWork")
return if (AnilistNotificationTask().execute(applicationContext)) {
if (System.currentTimeMillis() - lastCheck < 60000) {
Logger.log("SubscriptionNotificationWorker: doWork skipped")
return Result.success()
}
lastCheck = System.currentTimeMillis()
return if (SubscriptionNotificationTask().execute(applicationContext)) {
Result.success()
} else {
Logger.log("SubscriptionNotificationWorker: doWork failed")
@ -23,5 +28,6 @@ class SubscriptionNotificationWorker(appContext: Context, workerParams: WorkerPa
val checkIntervals = arrayOf(0L, 480, 720, 1440)
const val WORK_NAME =
"ani.dantotsu.notifications.subscription.SubscriptionNotificationWorker"
private var lastCheck = 0L
}
}