jkane001 jkane001 - 1 year ago 168
Android Question

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:

<action android:name="com.google.android.gms.gcm.ACTION_TASK_READY"/>

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()


And I have MyService, which looks like:

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

return GcmNetworkManager.RESULT_SUCCESS;

public int onStartCommand (Intent intent, int flags, int startId) {


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?


Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download