7

I am trying to use GCM Network Manager to send logs to out backend service. We have an alarm running about every hour that creates a OneoffTask that, when executed, will call the backend service with the log message.

This works, BUT a very large amount of the Tasks are lost (way more than half). At first, I thought it has something to do with our backend, or the network, but after adding a ton of file logging, it turns out that onRunTask in the service is never triggered for these tasks (but they are definitely getting scheduled. Why are these lost? Am I misunderstanding the API, or is OneoffTasks simply not reliable?

This is how the OneoffTask is scheduled:

GcmNetworkManager.getInstance(context).schedule(new OneoffTask.Builder()
    .setService(AvroLogService.class)
    .setExtras(bundle)

    // A mandatory tag which identifies the task
    // We add a unique hash to the tag to make sure that
    // tasks are logged and not thrown away as dupes.
    // See: http://stackoverflow.com/q/34528960/304262
    .setTag(java.util.UUID.randomUUID().toString())

    // Persist to disk, even across boots:
    .setPersisted(true)

    // Sets a time frame for the execution of this task in seconds.
    // This specifically means that the task can either be
    // executed right now, or at latest at a certain point:
    .setExecutionWindow(0, TWO_WEEKS_IN_SECONDS)
    .build());

Again, not this works, but only part of the messages. For the messages that are subsequently lost, the above code is definitely executed (I've added file logging to verify this), but there is never a corresponding onRunTask triggered for the lost ones.

I have verified that:

  1. Manifest is updated according to the Network Manager Implementation Guide (https://developers.google.com/cloud-messaging/network-manager)
  2. AvroLogService (my service) extends GcmTaskService
  3. It overrides onRunTask
  4. The app has RECEIVE_BOOT_COMPLETED permission.
  5. AvroLogService does NOT override onStartCommand.

I'm lost. Can someone share insights on this?

Mattias Petter Johansson
  • 1,064
  • 1
  • 15
  • 32
  • You may want to check this [GitHub post](https://github.com/google/gcm/issues/67) for additional insights and examples on the use of `GcmNetworkManager`. If you haven't done so, it was mentioned in the post about the need to provide the logic for your task execution inside the `onRunTask()` and you have to keep track of the state that you need for that particular task to do what you want. – Teyam Jan 18 '17 at 11:44
  • Thank you for trying to help, but that post did not provide any relevant insights for this issue. The post talks about passing data to the tasks, and I am using the setExtras functionality (which didn't seem to exist when the post in question was written), but it's not really relevant to this issue. The problem is that the code inside of onRunTask is intermittently not executed. I.e. it's executed lots of times, but it also lose many of the queued tasks. It's like a big percentage of the tasks are simply lost and never reach onRunTask, even though they are successfully queued. – Mattias Petter Johansson Jan 24 '17 at 16:36
  • give us your `adb shell dumpsys activity service GcmService`, type this in your terminal. When other apps also use GCM network manager they can block your request. – Mysterious_android Apr 27 '18 at 06:42

2 Answers2

0

As I guess your constant TWO_WEEKS_IN_SECONDS really means 2 WEEKS. In this case you task is eligible for execution in any point of time from now to 2 WEEKS. So, this task doesn't have to be executed every hour. Try to set execution window in range of one hour or even less (.setExecutionWindow(0, HALF_AN_HOUR_IN_SECONDS))

See google api docs

Roman_D
  • 4,680
  • 2
  • 14
  • 17
0

As answer above execution time range may be to big. Also I think that You want execute event periodically try use PeriodicTask.Builder instead ofOneoffTask.Builder

bongo
  • 733
  • 4
  • 12
  • Yes, PeriodicTask may be good replacement, but be noticed that it can be not executed during Doze. – Roman_D Jan 31 '17 at 13:11