Skip to content

Instantly share code, notes, and snippets.

@SurajBahadur
Created January 14, 2020 13:07
Show Gist options
  • Save SurajBahadur/e9a49151949454bcb74b6c25cbbfa10e to your computer and use it in GitHub Desktop.
Save SurajBahadur/e9a49151949454bcb74b6c25cbbfa10e to your computer and use it in GitHub Desktop.
Showing display after 15 minutes using work manager
class ScheduleVersesOfDay(private var context: Context,private var workerParams: WorkerParameters) : Worker(context, workerParams) {
override fun doWork(): Result {
Log.e(TAB, "PeriodicWork in BackGround")
sendNotification("Work Manager", "This is the scheduled notification")
return Result.success()
}
companion object {
private val TAB = ScheduleVersesOfDay::class.java.simpleName
}
fun sendNotification(title: String?, message: String?) {
val notificationManager = applicationContext.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val channel =
NotificationChannel("default", "Default", NotificationManager.IMPORTANCE_DEFAULT)
notificationManager.createNotificationChannel(channel)
}
val notification: NotificationCompat.Builder = NotificationCompat.Builder(applicationContext, "default")
.setContentTitle(title)
.setContentText(message)
.setSmallIcon(R.drawable.logo)
notificationManager.notify(1, notification.build())
}
}
///////////////////////////////////////FOR STARTING THE NOTIFICATION////////////////////////////////////
fun startWorkManager(){
var mPeriodicWorkRequest: PeriodicWorkRequest = PeriodicWorkRequest.Builder(
ScheduleVersesOfDay::class.java,20, TimeUnit.MINUTES)
.addTag("periodicWorkRequest")
.build()
WorkManager.getInstance().enqueue(mPeriodicWorkRequest);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment