Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GCM Network Manager - Periodic Task not firing

I'm attempting to use the GcmNetworkManager to schedule a recurring task in my app, which runs down to API level 17. I've set everything up as explained on the GCM Network Manager page (https://developers.google.com/cloud-messaging/network-manager):

In my AndroidManifest.xml, I have:

<service
    android:name=".services.MyService"
    android:exported="true"
    android:permission="com.google.android.gms.permission.BIND_NETWORK_TASK_SERVICE">
    <intent-filter>
        <action android:name="com.google.android.gms.gcm.ACTION_TASK_READY"/>
    </intent-filter>
</service>

In my Application, I have:

long periodSecs = 30L; // the task should be executed every 30 seconds
long flexSecs = 15L; // the task can run as early as -15 seconds from the scheduled time

String tag = "myScan|1";

PeriodicTask periodic = new PeriodicTask.Builder()
        .setService(MyService.class)
        .setPeriod(periodSecs)
        .setFlex(flexSecs)
        .setTag(tag)
        .setPersisted(false)
        .setRequiredNetwork(com.google.android.gms.gcm.Task.NETWORK_STATE_ANY)
        .setRequiresCharging(false)
        .setUpdateCurrent(true)
        .build();

GcmNetworkManager.getInstance(this).schedule(periodic);

And I have MyService, which looks like:

public class MyService extends GcmTaskService {
    @Override
    public int onRunTask(TaskParams taskParams) {
        Log.info("onRunTask: " + taskParams.getTag());

        return GcmNetworkManager.RESULT_SUCCESS;
    }

    @Override
    public int onStartCommand (Intent intent, int flags, int startId) {
        Log.info("onStartCommand");

        return GcmTaskService.START_STICKY_COMPATIBILITY;
    }
}

When I start the app, I get the onStartCommand logged as expected, but onRunTask never gets called. Am I missing something? I'm expecting that, once started (as evidenced by the start command firing), it should run every 15-30 seconds - is that a correct assumption? Why isn't it firing at all?

Thanks!

like image 642
jkane001 Avatar asked Jul 14 '15 02:07

jkane001


2 Answers

The problem is that you are overriding onStartCommand! This is how Google Play Services executes the task on your GcmTaskService. If you want it to work simply

return super.onStartCommand(intent, flags, startId);

Perhaps it's worth mentioning that the reason onRunTask is provided is so you don't have to worry about your Service's lifecycle - you can rely on the internals of GcmTaskService to stopService when required.

That said, if you startService() on your GcmTaskService with custom intents you will likely mess this up and end up with a Service that isn't stopped when it should be.

If you do need to call in to the GcmTaskService (not recommended) you should bind to it - that interface is completely untouched by the GcmTaskService internals.

like image 149
43matthew Avatar answered Oct 15 '22 10:10

43matthew


@Override
public int onRunTask(TaskParams taskParams)
{
    Log.info("onRunTask: " + taskParams.getTag());

    return GcmNetworkManager.RESULT_SUCCESS;
}

@Override
public void onCreate()
{
    Log.i(TAG, "in onCreate");
    super.onCreate();
    GcmNetworkManager.getInstance(this).schedule(createPeriodicSyncTask());
}

private PeriodicTask createPeriodicSyncTask()
{
    return new PeriodicTask.Builder()
            .setService(PeriodicSyncService.class)
            .setPeriod(mSyncIntervalInSec)
            .setFlex(mSyncIntervalInSec - 20)
            .setTag(TAG)
            .setPersisted(true)
            .setRequiredNetwork(NETWORK_STATE_ANY)
            .setUpdateCurrent(true)
            .setRequiresCharging(false)
            .build();
}
like image 1
bitsabhi Avatar answered Oct 15 '22 11:10

bitsabhi