androidjobservice

Firing JobService without constraints


I have small problem when on some devices my notification that is no background not fires sometimes on android O (Particularly on Xiaomi). I am using Awareness API which I receive data from BroadcastReceiver .So... what I thought to do is put my notification in JobService and maybe get some of those processing time, and then call it like so:

PersistableBundle bundle = new PersistableBundle();
            bundle.putString("KEY1", "Test");

            JobInfo job = new JobInfo.Builder(0, new ComponentName(context, AppNotification.class))
                    .setRequiredNetworkType(JobInfo.NETWORK_TYPE_ANY)
                    .setExtras(bundle)
                    .build();

            JobScheduler jobScheduler = (JobScheduler) getSystemService(Context.JOB_SCHEDULER_SERVICE);
            jobScheduler.schedule(job);

But when I run it without

.setRequiredNetworkType(JobInfo.NETWORK_TYPE_ANY)

I recive an error "You're trying to build a job with no constraints, this is not allowed". But I do not need any constraints, and this is the one I found most likely to always work. But what is the constraints that actually always work? How can I fire JobService without any constraints?


Solution

  • I am using .setOverrideDeadline(0). It basically runs the job as soon as it is scheduled and I see no harm in it. I think it is a better constraint than network as it is always satisfied, whereas it is impossible for a user to have network 100% of the time.

    I have found no way to set no constraints successfully.