Рабочий класс -:
Код: Выделить всё
@HiltWorker
class NewsWorker @AssistedInject constructor(
@Assisted appContext: Context,
@Assisted params: WorkerParameters,
) : CoroutineWorker(appContext, params) {
override suspend fun doWork(): Result {
val newsRepository = ServiceLocator.newsRepository
Log.d("worker call", "this is a worker call")
var result: Result? = null
try {
newsRepository.workManagerRequest()
.catch {
result = Result.failure()
Log.d("worker call", "this is a worker call failure from 1st catch")
}
.collect { flowResult ->
when (flowResult) {
is Results.Success -> {
Log.d("worker call", "this is a worker call Success")
// Handle the successful case
// You can process the PagingData here
showNotification()
}
is Results.Error -> {
Log.d("worker call", "this is a worker call failure from Result.Error")
// Handle the error case
result = Result.failure()
}
}
if (result != null) return@collect
}
} catch (e: Exception) {
Log.d("worker call", "this is a worker call failure from second catch :$e")
result = Result.failure()
}
return result ?: Result.success()
}
private fun showNotification() {
val notificationManager = NotificationManagerCompat.from(applicationContext)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
if (ContextCompat.checkSelfPermission(
applicationContext,
Manifest.permission.ACCESS_NOTIFICATION_POLICY
) == PackageManager.PERMISSION_GRANTED
) {
// Permission is granted, proceed with creating the channel
val channel = NotificationChannel(
"News Channel ID",
"News Channel",
NotificationManager.IMPORTANCE_DEFAULT
)
notificationManager.createNotificationChannel(channel)
} else {
// Permission is not granted, show a dialog to ask for permission
return
}
}
val notificationBuilder = NotificationCompat.Builder(applicationContext, "News Channel ID")
.setContentTitle("News")
.setContentText("You have new updated news please check it out")
.setSmallIcon(R.drawable.news)
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
notificationManager.notify(1, notificationBuilder.build())
}
}
object ServiceLocator {
lateinit var newsRepository: NewsRepository
}
Код: Выделить всё
@HiltAndroidApp
class NewsApp: Application() , Configuration.Provider{
@Inject
lateinit var hiltWorkerFactory: HiltWorkerFactory
override fun onCreate() {
super.onCreate()
setUpWorker()
}
override val workManagerConfiguration: Configuration
get() = Configuration.Builder()
.setWorkerFactory(hiltWorkerFactory)
.build()
private fun setUpWorker() {
val constraints = Constraints.Builder()
.build()
val workRequest = PeriodicWorkRequestBuilder(15, TimeUnit.MINUTES)
.setConstraints(constraints)
.build()
WorkManager.getInstance().enqueueUniquePeriodicWork(
NewsWorker::class.java.name,
ExistingPeriodicWorkPolicy.KEEP,
workRequest
)
}
}
Manifest -:
android:name="androidx.startup.InitializationProvider"
android:authorities="${applicationId}.androidx-startup"
android:exported="false"
tools:node="merge">
Dependecy-:
//dagger-hilt for worker
implementation ("androidx.hilt:hilt-work:1.0.0")
kapt ("androidx.hilt:hilt-compiler:1.0.0")
//Work manager
implementation ("androidx.work:work-runtime-ktx:2.7.1")
`
Подробнее здесь: https://stackoverflow.com/questions/783 ... t-app-list