1

Let's say my application receives a push notification from a server that carries an item ID. After receiving the push notification item's details have to be fetched from the server (using retrofit) and after that an Android notification is displayed. This is pretty straightforward when a phone is not idle.

But what happens when, e.g. I lock the phone and keep it that way? After a time the phone goes to sleep. Push notification should be received. But what bugs my mind is the retrofit call. Will the application stay awake till I finish - dispose - the retrofit call or it can be "killed" while handling it? Does it have to do something with so called WakeLock? Or is it a bad way to use retrofit in a first place? I couldn't find any good article or example that will help me understand.

peter.o
  • 3,460
  • 7
  • 51
  • 77
  • If you have a solution, you should post it as an answer. Please do not edit your question for this. Please consider removing the solution from your question, and post it as an answer instead. – GrumpyCrouton Jan 31 '18 at 19:21

2 Answers2

0

Sounds like a job for the SyncAdapter. I have never experienced it being killed prematurely although as anything it can be. It already runs on a separate process, so there is no need for calling .subscribeOn(Schedulers.io()). The documentation provides an example which exactly describes your use case. Meaning synchronization after a GCM notification. I'm not going to copy it here you'll find it under the link above.

Lukasz
  • 2,257
  • 3
  • 26
  • 44
  • Thanks for the answer. I managed it using Evernote's Android Job library (there is also an option with FirebaseJob - mentioned in code samples on FCM documentation page). With Android Job I schedule call after I receive the push notification. Had to use blocking RX calls so result wouldn't overtake retrofit response: https://github.com/evernote/android-job/wiki/FAQ#how-can-i-run-async-operations-in-a-job – peter.o Jan 31 '18 at 19:16
0

My solution: I managed it using Evernote's Android Job library (there is also an option with FirebaseJob - mentioned in code samples on FCM documentation page). With Android Job I schedule call after I receive the push notification. Had to use blocking RX calls so the result wouldn't overtake retrofit response: https://github.com/evernote/android-job/wiki/FAQ#how-can-i-run-async-operations-in-a-job

peter.o
  • 3,460
  • 7
  • 51
  • 77