4

I try to get rid of IntentService when handle GCM as described here. Because of Android O background limitation. But i can't pass Bundle extras with push info as parameter to WorkManager from BroadcastReceiver. Is there any way to put Bundle into WorkManager Data?

Gogi Bobina
  • 1,086
  • 1
  • 8
  • 25

1 Answers1

6

I solve this problem in a simple way. I noticed, that the whole parameters of Bundle are Strings, so i just put them into Data in broadcastReceiver and convert back in Worker.

class GcmWorker : Worker() {
    companion object {
        fun createWork(extras: Bundle): OneTimeWorkRequest {
            val dataBuilder = Data.Builder()
            extras.keySet().forEach {
                dataBuilder.putString(it, extras.getString(it))
            }

            return OneTimeWorkRequest.Builder(GcmWorker::class.java)
                    .setInputData(dataBuilder.build())
                    .build()
        }
    }

    override fun doWork(): Result {
        val extras = Bundle()
        for (key in inputData.keyValueMap.keys) {
            extras.putString(key, value)
        }
        // ...
        return WorkResult.FAILURE
    }
}
Gogi Bobina
  • 1,086
  • 1
  • 8
  • 25