3

Background info

So I have created an app that is mainly controlled by a notification that's supported by a service. This notification disapears when a user loses connection to a specific WiFi network, and has to appear when the user get's back home and connects to the Wifi network. Before Android O I just let the service run in the background. The service was bound to a broadcastreceiver that would tell the service whenever WiFi was connected and disconnected. The service would then act upon those changes.

The breaking change

Now we have Android Oreo with the background limitations. Because of this, and to preserve some battery, I have changed the app to stop the service whenever WiFi is disconnected, and start a Firebase Jobdispatcher job. This job will be executed whenever Android feels like it. So it may very well be that the user is connected to WiFi, and 15 minutes later the job gets executed.

The problem

I want the job to get executed immediately when the phone gets connected to a WiFi network, without having an service running. I know its possible, because I recently saw this exact same behavior in the Google Translate Android app. When you enable a language for offline translation, it only get's downloaded when WiFi is connected. Even when I close all of its services (in Developer options), it reacts immediately when I connect to WiFi.

Any ideas?

For anyone interested, here's my job config:

    Job myJob = mDispatcher.newJobBuilder()
            .setService(ScheduledJobService.class)
            .setTag(JOB_TAG)
            .setRecurring(true)
            .setTrigger(Trigger.executionWindow(1, 10))
            .setLifetime(Lifetime.UNTIL_NEXT_BOOT)
            .setReplaceCurrent(true)
            .setConstraints(Constraint.ON_UNMETERED_NETWORK)
            .setRetryStrategy(RetryStrategy.DEFAULT_LINEAR)
            .build();
    mDispatcher.mustSchedule(myJob);
Beuz
  • 193
  • 1
  • 12

1 Answers1

0

This does the job very well. This will run immediately when the phone gets connected to WiFi.

public void startNetworkChangeJob(){
    // Create a new dispatcher using the Google Play driver.
    FirebaseJobDispatcher dispatcher = new FirebaseJobDispatcher(new GooglePlayDriver(mContext));

    Job myJob = dispatcher.newJobBuilder()
            // the JobService that will be called
            .setService(NetworkChangeJob.class)
            // uniquely identifies the job
            .setTag(mContext.getString(R.string.networkChangeJobId))
            // recurring job
            .setRecurring(true)
            // persist past a device reboot
            .setLifetime(Lifetime.FOREVER)
            // start between 0 and 60 seconds from now
            .setTrigger(Trigger.executionWindow(0, 60))
            // don't overwrite an existing job with the same tag
            .setReplaceCurrent(false)
            // retry with exponential backoff
            .setRetryStrategy(RetryStrategy.DEFAULT_EXPONENTIAL)
            // constraints that need to be satisfied for the job to run
            .setConstraints(
                    // only run on an unmetered network
                    Constraint.ON_UNMETERED_NETWORK
            )
            .build();

    dispatcher.mustSchedule(myJob);
}
Beuz
  • 193
  • 1
  • 12
  • 1
    Edit: it sometimes works, but I really need a solution that works all the time. Like the YouTube app, whenever I get home and someone is streaming to the tv I get a notification letting me know someone is streaming, and it works all the time. How do they do it? – Beuz Nov 29 '18 at 14:37
  • any news on this? I am in a very similar situation trying to launch notifications when the user connects to a specific WIFI SSID – Cesar Dec 25 '18 at 22:14
  • Unfortunately not. Probably the YouTube app has some special permissions in Android. I still don't have a reliable way to do this. – Beuz Jan 03 '19 at 11:01
  • I got it working as @CommonsWare suggested [here](https://stackoverflow.com/q/53926054/1818267) – Cesar Jan 03 '19 at 14:20
  • Yes that's what I'm offering the user now, next to the less reliable Android Worker. – Beuz Jan 04 '19 at 08:14