Created
January 14, 2020 13:07
-
-
Save SurajBahadur/e9a49151949454bcb74b6c25cbbfa10e to your computer and use it in GitHub Desktop.
Showing display after 15 minutes using work manager
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
///////////////////////////////////////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