How to enqueue WorkManager OneTimeWorkRequest at specific time
Asked Answered
E

2

4

I have a scenario where i have to give user notification using WorkManager at specific time.

How can i schedule Work at specific time, and it should show up even if user force kills the app. or app is not running.

My Current code is as below :

Data d = new Data.Builder().putInt(IntentConstants.SCHEDULE_ID, scheduleData.getScheduleID()).build();
OneTimeWorkRequest compressionWork =
    new OneTimeWorkRequest.Builder(ScheduleWorker.class)
        .setInputData(d)
        .build();
WorkManager.getInstance().enqueue(compressionWork);
Edmead answered 5/8, 2018 at 11:18 Comment(1)
You can't fire a WorkManager work at an exact time. But you can make sure you are white-listed by using a high-priority FCM, and enqueue (and run) the work then. Another option is the AlarmManager setExactAndAllowWhileIdle() method. Look here (Doze and App Standby section) - google-developer-training.gitbooks.io/…Wappes
E
9

I think i have found an easy and working solution. It works 100%.

We have to get current time in milliseconds and required specific time to trigger it in milliseconds then you have to calculate specific time - current time.

I have my solution (working code below) :

Data d = new Data.Builder()
             .putInt(IntentConstants.SCHEDULE_ID, scheduleData.getScheduleID())
             .build();

long currentTime= System.currentTimeMillis();
long specificTimeToTrigger = c.getTimeInMillis();
long delayToPass = specificTimeToTrigger - currentTime;

OneTimeWorkRequest compressionWork =
                        new OneTimeWorkRequest.Builder(ScheduleWorker.class)
                                .setInputData(d)
                                .setInitialDelay(delayToPass, TimeUnit.MILLISECONDS)
                                .build();

WorkManager.getInstance().enqueue(compressionWork);

Main Logic is in the delayToPass = currentTime (in Millis) - specificTimeToTrigger (in Millis)

Edmead answered 5/8, 2018 at 14:0 Comment(0)
B
0
long currentTime= System.currentTimeMillis();
long specificTimeToTrigger = c.getTimeInMillis();
long delayToPass = specificTimeToTrigger - currentTime;

OneTimeWorkRequest compressionWork =
                        new OneTimeWorkRequest.Builder(ScheduleWorker.class)
                                .setInitialDelay(delayToPass, TimeUnit.MILLISECONDS)
                                .build();

WorkManager.getInstance().enqueue(compressionWork);

It's trigger correct on selected time when selected time > current time. If selected time < current time it triggers immediate.

To overcome immediate trigger, You can use below condition to trigger next day if selected selected time is less than current time.

if ((specificTimeToTrigger - currentTime) < 0) {
    delayToPass = (86400000)-(currentTime - specificTimeToTrigger);
    //24hr-(currentTime - specificTimeToTrigger)
} else {
    delayToPass = specificTimeToTrigger - currentTime;
}
Blotch answered 5/7, 2021 at 13:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.