Android-job: Issue while scheduling Daily Jobs using 1.2.0

Created on 4 Oct 2017  路  7Comments  路  Source: evernote/android-job

I am using 1.2.0-RC5 version of the library to schedule a daily job. I followed this link and everything is working fine and scheduled job of sending notification is shown but when I click on notification and come back to my activity I am getting these logs.

E/JobExecutor: Crashed job{id=1, finished=true, result=FAILURE, canceled=false, periodic=false, class=DailySyncJob, tag=DailySyncJob} java.lang.IllegalArgumentException: startInMs must be greater than 0 at com.evernote.android.job.util.JobPreconditions.checkArgumentPositive(JobPreconditions.java:165) at com.evernote.android.job.JobRequest$Builder.setExecutionWindow(JobRequest.java:714) at com.evernote.android.job.DailyJob.schedule(DailyJob.java:102) at com.evernote.android.job.DailyJob.onRunJob(DailyJob.java:141) at com.evernote.android.job.Job.runJob(Job.java:121) at com.evernote.android.job.JobExecutor$JobCallable.runJob(JobExecutor.java:165) at com.evernote.android.job.JobExecutor$JobCallable.call(JobExecutor.java:150) at com.evernote.android.job.JobExecutor$JobCallable.call(JobExecutor.java:133) at java.util.concurrent.FutureTask.run(FutureTask.java:237) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607) at java.lang.Thread.run(Thread.java:761)

Major concern is that my Daily job that is already scheduled is getting rescheduled.
Let me know if anyone find any solution of this.

bug

All 7 comments

That's a bug, can you share the code how you schedule your job?

Here is the Class that I have created extending DailyJob.

`public class DailySyncJob extends DailyJob {

public static final String TAG = "DailySyncJob";

public static void schedule() {
    if (!JobManager.instance().getAllJobRequestsForTag(TAG).isEmpty()) {
        // job already scheduled, nothing to do
      return;
    }


    JobRequest.Builder builder = new JobRequest.Builder(TAG);

    // run job between 1pm and 2pm
    DailyJob.schedule(builder, TimeUnit.HOURS.toMillis(13), TimeUnit.HOURS.toMillis(14));
}

@NonNull
@Override
protected DailyJobResult onRunDailyJob(Job.Params params) {
    // do something

    PendingIntent pi = PendingIntent.getActivity(getContext(), 0,
            new Intent(getContext(), MainActivity.class), 0);

    Notification notification = new NotificationCompat.Builder(getContext())
            .setContentTitle("Android DailyJob Sample")
            .setContentText("Notification from Android Job Sample App.")
            .setAutoCancel(true)
            .setContentIntent(pi)
            .setSmallIcon(R.mipmap.ic_launcher)
            .setShowWhen(true)
            .setColor(Color.RED)
            .setLocalOnly(true)
            .build();

    NotificationManagerCompat.from(getContext())
            .notify(new Random().nextInt(), notification);

    return DailyJobResult.SUCCESS;
}

}`

I am calling it from MainActivity.class by..

DailySyncJob.schedule();

Change


        if (startMs > endMs) {
            // e.g. when job should run between 10pm and 2am
           endMs  += TimeUnit.DAYS.toMillis(1);
        }
        long endDelay = startDelay + (endMs - startMs);

To this in DailyJob.java in lib


        Long tempEnd = endMs;
        if (startMs > endMs) {
            // e.g. when job should run between 10pm and 2am
            tempEnd += TimeUnit.DAYS.toMillis(1);
        }
        long endDelay = startDelay + (tempEnd - startMs);

This worked for me, I hope it does for you too

thanks @thisismohitgupta will try this. Also is it possible to give exact time for job scheduling or less span of time like between 2:00pm to 2:05pm (Hours and minutes both) as I wanted notification should reach to all the user at somewhat similar time.

@anukools I think this depends on the OS and there are many other factors involved. I think you should look into firebase cloud messaging for notifications its more robust

@thisismohitgupta my notification are in-app notification like reminders that should be shown offline also. So I thought Is there any workaround to give minimum interval in start/end time.

@thisismohitgupta This workaround is not needed anymore.

@anukools Thanks again for reporting. The issue is fixed in RC6 (which should be available in 30 minutes).

Was this page helpful?
0 / 5 - 0 ratings