fix: notification check on app launch
This commit is contained in:
parent
dca6ffdbbe
commit
a189802061
7 changed files with 50 additions and 4 deletions
|
@ -121,7 +121,9 @@ class App : MultiDexApplication() {
|
||||||
}
|
}
|
||||||
|
|
||||||
val useAlarmManager = PrefManager.getVal<Boolean>(PrefName.UseAlarmManager)
|
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() {
|
private fun setupNotificationChannels() {
|
||||||
|
|
|
@ -15,7 +15,7 @@ import java.util.concurrent.TimeUnit
|
||||||
|
|
||||||
class AlarmManagerScheduler(private val context: Context) : TaskScheduler {
|
class AlarmManagerScheduler(private val context: Context) : TaskScheduler {
|
||||||
override fun scheduleRepeatingTask(taskType: TaskType, interval: Long) {
|
override fun scheduleRepeatingTask(taskType: TaskType, interval: Long) {
|
||||||
if (interval < TimeUnit.MINUTES.toMillis(15)) {
|
if (interval * 1000 < TimeUnit.MINUTES.toMillis(15)) {
|
||||||
cancelTask(taskType)
|
cancelTask(taskType)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
package ani.dantotsu.notifications
|
package ani.dantotsu.notifications
|
||||||
|
|
||||||
import android.content.Context
|
import android.content.Context
|
||||||
|
import androidx.work.OutOfQuotaPolicy
|
||||||
import ani.dantotsu.notifications.anilist.AnilistNotificationWorker
|
import ani.dantotsu.notifications.anilist.AnilistNotificationWorker
|
||||||
import ani.dantotsu.notifications.comment.CommentNotificationWorker
|
import ani.dantotsu.notifications.comment.CommentNotificationWorker
|
||||||
import ani.dantotsu.notifications.subscription.SubscriptionNotificationWorker
|
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 {
|
companion object {
|
||||||
fun create(context: Context, useAlarmManager: Boolean): TaskScheduler {
|
fun create(context: Context, useAlarmManager: Boolean): TaskScheduler {
|
||||||
return if (useAlarmManager) {
|
return if (useAlarmManager) {
|
||||||
|
|
|
@ -10,7 +10,7 @@ import ani.dantotsu.notifications.subscription.SubscriptionNotificationWorker
|
||||||
|
|
||||||
class WorkManagerScheduler(private val context: Context) : TaskScheduler {
|
class WorkManagerScheduler(private val context: Context) : TaskScheduler {
|
||||||
override fun scheduleRepeatingTask(taskType: TaskType, interval: Long) {
|
override fun scheduleRepeatingTask(taskType: TaskType, interval: Long) {
|
||||||
if (interval < PeriodicWorkRequest.MIN_PERIODIC_INTERVAL_MILLIS) {
|
if (interval * 1000 < PeriodicWorkRequest.MIN_PERIODIC_INTERVAL_MILLIS) {
|
||||||
cancelTask(taskType)
|
cancelTask(taskType)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
|
@ -10,6 +10,11 @@ class AnilistNotificationWorker(appContext: Context, workerParams: WorkerParamet
|
||||||
|
|
||||||
override suspend fun doWork(): Result {
|
override suspend fun doWork(): Result {
|
||||||
Logger.log("AnilistNotificationWorker: doWork")
|
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)) {
|
return if (AnilistNotificationTask().execute(applicationContext)) {
|
||||||
Result.success()
|
Result.success()
|
||||||
} else {
|
} else {
|
||||||
|
@ -21,5 +26,6 @@ class AnilistNotificationWorker(appContext: Context, workerParams: WorkerParamet
|
||||||
companion object {
|
companion object {
|
||||||
val checkIntervals = arrayOf(0L, 30, 60, 120, 240, 360, 720, 1440)
|
val checkIntervals = arrayOf(0L, 30, 60, 120, 240, 360, 720, 1440)
|
||||||
const val WORK_NAME = "ani.dantotsu.notifications.anilist.AnilistNotificationWorker"
|
const val WORK_NAME = "ani.dantotsu.notifications.anilist.AnilistNotificationWorker"
|
||||||
|
private var lastCheck = 0L
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -11,6 +11,11 @@ class CommentNotificationWorker(appContext: Context, workerParams: WorkerParamet
|
||||||
CoroutineWorker(appContext, workerParams) {
|
CoroutineWorker(appContext, workerParams) {
|
||||||
override suspend fun doWork(): Result {
|
override suspend fun doWork(): Result {
|
||||||
Logger.log("CommentNotificationWorker: doWork")
|
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)) {
|
return if (CommentNotificationTask().execute(applicationContext)) {
|
||||||
Result.success()
|
Result.success()
|
||||||
} else {
|
} else {
|
||||||
|
@ -30,5 +35,6 @@ class CommentNotificationWorker(appContext: Context, workerParams: WorkerParamet
|
||||||
companion object {
|
companion object {
|
||||||
val checkIntervals = arrayOf(0L, 480, 720, 1440)
|
val checkIntervals = arrayOf(0L, 480, 720, 1440)
|
||||||
const val WORK_NAME = "ani.dantotsu.notifications.comment.CommentNotificationWorker"
|
const val WORK_NAME = "ani.dantotsu.notifications.comment.CommentNotificationWorker"
|
||||||
|
private var lastCheck = 0L
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -11,7 +11,12 @@ class SubscriptionNotificationWorker(appContext: Context, workerParams: WorkerPa
|
||||||
|
|
||||||
override suspend fun doWork(): Result {
|
override suspend fun doWork(): Result {
|
||||||
Logger.log("SubscriptionNotificationWorker: doWork")
|
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()
|
Result.success()
|
||||||
} else {
|
} else {
|
||||||
Logger.log("SubscriptionNotificationWorker: doWork failed")
|
Logger.log("SubscriptionNotificationWorker: doWork failed")
|
||||||
|
@ -23,5 +28,6 @@ class SubscriptionNotificationWorker(appContext: Context, workerParams: WorkerPa
|
||||||
val checkIntervals = arrayOf(0L, 480, 720, 1440)
|
val checkIntervals = arrayOf(0L, 480, 720, 1440)
|
||||||
const val WORK_NAME =
|
const val WORK_NAME =
|
||||||
"ani.dantotsu.notifications.subscription.SubscriptionNotificationWorker"
|
"ani.dantotsu.notifications.subscription.SubscriptionNotificationWorker"
|
||||||
|
private var lastCheck = 0L
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Add table
Add a link
Reference in a new issue