2

I am trying to build a chat app using XMPP + PHP home made API + Local Android database. I wanted to use XMPP running in the background (as a service or a background Job) to tell my app to fetch data from the API if a new message arrived. Then store it in the local database, and update my messages box with the updated content.

The XMPP client is working fine in foreground (Lolipop and OREO) But when I close the app (slide to the side, not hard kill), it still works for maybe 5 minutes then it idle and 2 min later back up again for 5 min and so on. (On Lolipop). And it immidiately stops on OREO.

I know that OREO needs Job Scheduler to manage my background task. But why does my service stops after 5 minutes on Lolipop ?

I tried to override the onDestroy method of my Service:
https://stackoverflow.com/a/43936309/10651475
but it doesn't changes anything.

And how should I implement my Background Job on OREO ? The job is running once, and can be restarted periodically but I want to keep it running in order to receive new messages immidiately even if I have not been using my phone for 2 hours.

WhatsApp, Facebook Messanger, Snapchat and Instagram are doing all this perfectly on both versions (Lolipop & OREO). But how they do this ???

I forgot to mention that all those apps don't have a pending notification displayed.

I read a lot a discussions about this topic. It seams that they use Push Notifications, GoogleCloudMessaging etc. But I feel like I’m forced to use GCM, Firebase and all this Ecosystem.

Should I understand that Google is locking Android years after years ?

https://medium.com/exploring-android/exploring-background-execution-limits-on-android-oreo-ab384762a66c

https://android.stackexchange.com/questions/140669/how-does-whatsapp-stay-in-background-without-being-closed

https://android.jlelse.eu/keep-those-background-services-working-when-targeting-android-oreo-sdk-26-cbf6cc2bdb7f

How does whatsapp Keeps its eJabberd/XMPP server connected when the app is Closed?

I read those articles and much more, but I didn’t find my answer there...

If you have an idea, it would help me a lot.

Thank you :)

Milos
  • 21
  • 3
  • Android Jetpack has introduced the work manager but it's still in alpha release (https://developer.android.com/topic/libraries/architecture/workmanager/). Supposedly that will choose the best method to use depending on the device's version – Nikos Hidalgo Nov 14 '18 at 14:45
  • Thank you ! I heard about that, but it is "just" a way to avoid compatibility problems. I don't think it solves the idle part.. – Milos Nov 14 '18 at 15:26
  • Same issue here. – Praveen B. Bhati Nov 26 '18 at 09:19

1 Answers1

0

for your lollipop issue try

@Override
public void onTaskRemoved(Intent rootIntent) {
    Intent intent = new Intent(this, XmppService.class);
    startService(intent);
}

in your service class.