Sunday, 11 April 2021

Network request is fetched multiple times in CoroutineWorker

I have setup the worker class, my intent was to fetch data from the web each 24 hours and i want to get a notification with fetched data. I used periodic work request. My app behavior is weird, notifications fire off only when i start the app. And when app is created, i receive multiple notifications with the data i want. I want only one notification each 24 hours.

Here is my worker class code

@RequiresApi(Build.VERSION_CODES.O)
class PeriodicWork(context: Context, workerParameters: WorkerParameters) :
    CoroutineWorker(context, workerParameters) {

    private val repository =
        Repository(Database.invoke(applicationContext))

    override suspend fun doWork(): Result {
        try {
            val notificationString = getNotificationResponse().body()!!.value

            val notification =
                NotificationCompat.Builder(applicationContext, NOTIFICATION_CHANNEL_ID)
                    .setSmallIcon(R.drawable.ic_drawable)
                    .setContentTitle("Notification")
                    .setContentText(notificationString)
                    .setStyle(
                        NotificationCompat.BigTextStyle()
                            .bigText(notificationString)
                    )
                    .setPriority(NotificationCompat.PRIORITY_HIGH)
                    .build()

            createNotificationChannel()
            with(NotificationManagerCompat.from(applicationContext)) {
                notify(1, notification)
            }
        } catch (e: Exception) {

        }

        return Result.success()
    }

    private fun createNotificationChannel() {
        val channel = NotificationChannel(
            NOTIFICATION_CHANNEL_ID,
            "New Notification",
            NotificationManager.IMPORTANCE_HIGH
        ).apply {
            description = "Notification channel"
        }

        val notificationManager =
            applicationContext.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
        notificationManager.createNotificationChannel(channel)
}

    private suspend fun getNotificationResponse(): Response<WantedData> {
        val notificationResponse =
            withContext(Dispatchers.IO) { repository.getRandomString() }
        repository.saveString(notificationResponse.body()!!)

        return notificationResponse
    }

}

MainActivity part of code where i instantiate worker in a function which i call in onCreate lifecycle method

    private fun setupPeriodicRequest() {

        val periodicRequest =
            PeriodicWorkRequestBuilder<PeriodicWork>(24, TimeUnit.HOURS)
                .build()

        WorkManager.getInstance()
            .enqueueUniquePeriodicWork(
                "Periodic Notification",
                ExistingPeriodicWorkPolicy.REPLACE,
                periodicRequest
            )
    }

If needed i can provide more info, thanks in advance!!!



from Network request is fetched multiple times in CoroutineWorker

No comments:

Post a Comment