1

I start an IntentService from MainActivity. This service is responsible to initialize FCM data objects (nodes) which listen to any changes done on FCM (cloud). When there is any data changes, a push notification is triggered.

Now since Android O does not allow service to be kept running in background, no push notification is observed. How to solve this issue?

Benjamin
  • 7,055
  • 6
  • 40
  • 60
Akshay Mahale
  • 63
  • 1
  • 7
  • Read [this](https://medium.com/exploring-android/exploring-background-execution-limits-on-android-oreo-ab384762a66c) article for background execution limit in Oreo. – Hemant Parmar Mar 20 '18 at 05:06
  • @HemantParmar the one using foreground service will provide the functionality but it will also show service running icon at the top. Remaining alternatives like Job Scheduler will not work for chat messaging. Thanks anyways! – Akshay Mahale Mar 20 '18 at 05:08
  • See my answer here for a similar question here - https://stackoverflow.com/questions/49063314/intentservice-stops-working-when-app-is-removed-from-recent-apps/49064710#49064710 – Asutosh Panda Mar 20 '18 at 05:30
  • Job dispatcher can be used for scheduled tasks. My chat messages will not be scheduled so Job dispatcher will not be a suitable solution. Even with foreground the problem is persistent notification which becomes annoying for the user. – Akshay Mahale Mar 20 '18 at 05:34

1 Answers1

0
  1. IntentService is not intended to run for long periods of time. In order to do long running tasks as observing something, you would normally use a Service
  2. Starting from Android O, even normal services are subject to background execution limitations. You can save your service from getting killed by system by qualifying it as a Foreground Service. The often simplest way to do that is by showing a persistent notification.
  3. As far as FCM is concerned, you can simply register your service for receiving FCM data messages and safely expect it to get called. This service will not even have to be a foreground service if your logic (in response to an FCM data message) is not considerably time consuming.
Abdul Wasae
  • 3,614
  • 4
  • 34
  • 56
  • Thanks! All your points sounds good, but in my chat application messages are delivered when data changes at specific points and Point 3 is not a solution to trach data changes in FCM nodes. – Akshay Mahale Mar 20 '18 at 05:38
  • What exactly do you mean by FCM Nodes? – Abdul Wasae Mar 20 '18 at 05:57
  • I mean to say referring to the Firebase realtime data base, where in we put Data change listener to track any data changed occurred, any child nodes deleted added updated etc. – Akshay Mahale Mar 20 '18 at 09:21
  • Oh ok. In case case you'll have to use point number 2. Either that, or change your logic, perhaps, such that instead of placing the data change listener in the app side, you do it in firebase cloud functions in the form of a trigger. And when that trigger fires, you send an FCM data message to the app in order to let it know about the change – Abdul Wasae Mar 20 '18 at 09:33
  • Yup cloud function is an option but irony is that it's not free – Akshay Mahale Mar 20 '18 at 13:21
  • Firebase functions are free upto a certain point, just like realtime db – Abdul Wasae Mar 20 '18 at 13:40
  • I implemented firebase cloud functions and it works on MOTOROLA devices perfectly. But when tried on OREO device or MI and app is not in background or killed no push notification is received. – Akshay Mahale Apr 08 '18 at 12:32
  • 1
    "This service will not even have to be a foreground service if your logic (in response to an FCM data message) is not considerably time consuming." Yeah sure: "For all messages where onMessageReceived is provided, your service should handle any message within 10 seconds of receipt. If your app needs more time to process a message, use the Firebase Job Dispatcher." Source: https://firebase.google.com/docs/cloud-messaging/android/receive :-( – Daniele Ricci Jul 30 '18 at 15:25