WorkManageršŸ”—Notification

Set notification for a specific date and time

Korshikov Alexey
3 min readMay 28, 2019

--

There are many options on Android for deferrable background work such as Firebase JobDispatcher, Job Scheduler and Alarm Manager + Broadcast receivers.

Why WorkManager?

WorkManager, a compatible, flexible and simple library for deferrable background work. WorkManager is the recommended task scheduler on Android for deferrable work, with a guarantee to be executed.

Key features:

  • Uses JobScheduler on devices with API 23+
  • Uses a combination of BroadcastReceiver + AlarmManager on devices with API 14ā€“22
  • Add work constraints like network availability or charging status
  • Schedule asynchronous one-off or periodic tasks
  • Monitor and manage scheduled tasks
  • Chain tasks together
  • Ensures task execution, even if the app or device restarts
  • Adheres to power-saving features like Doze mode

The WorkManager API makes it easy to schedule deferrable, asynchronous tasks that are expected to run even if the app exits or device restarts.

This example shows how to set notification for a specific date and time with WorkManager

To use WorkManager in your app

add the new dependency to your build.gradle file: implementation 'android.work:work-runtime:2.1.0-alpha02' or last version.

Create a base class of Worker:

and override the doWork() method:

Notifications are divided into two types:

To SDK_INT < Oreo and to SDK_INT ā‰„ Oreo

Notification (to SDK_INT < Oreo) has parameters such as large icon with bitmap from vector, small icon, Title, Subtitle, Priority MAX and

DEFAULT_ALL : Default vibration, sound and light.

<uses-permission android:name="android.permission.VIBRATE" /> 

Notification (to SDK_INT ā‰„ Oreo) has the same parameters, but need add channel with channel.vibration, sound, light and priority IMPORTANCE_HIGH.

Create the sendNotification() fun:

and add companion object :

Create vectorToBitmap() extension:

TimePicker are divided into two types:

To SDK_INT < Marshmallow and to SDK_INT ā‰„ Marshmallow

TimePicker (to SDK_INT ā‰„ Marshmallow) has methods such as

set/getHour and set/getMinute

TimePicker (to SDK_INT < Marshmallow) has methods such as

set/getCurrentHour and set/getCurrentMinute

Create a base class of TimePicker:

or add my TimePickerCompact dependency to your build.gradle file: implementation 'com.github.ifr0z:timepickercompact:1.0'.

DatePicker donā€™t has compatibility issues.

with

and add scheduleNotification() fun:

This WorkManager will enqueue and manage our work request.

ExistingWorkPolicy:

.REPLACE

Remove the existing work request and replace it with the new.

.KEEP

Save the existing work request and ignore new.

.APPEND

Add new work request to the existing, running the new sequenceā€™s first task after the existing sequenceā€™s last task finishes.

Results:

Android API 28 (P) ā€” Set notification for a specific date and time
Android API 28 (P) ā€” Fire a notification at a certain time

Thanks for reading this article :)

For more information about UI, visit this git repository.

Happy Coding !

Update (18.02.23):

Add check permission for notification

--

--

Korshikov Alexey

Open Source from ā„ļø with šŸ’™ on kšŸ˜¼tlin