I have read to many same questions here in StackOverflow and in Web but cannot configure something like "never ending service" or running even when app is removed from task (force killed). I am just wondering how services like Viber or WhatsUp working, because when we force killed those application we are still able to receive messages when someone write to us(So service is running still). I know about foreground service , but it is not solution because user don't want to see the notification. So here what i have tried. : This code in running inside services to detect real-time changes, and just wanted for this to stay active in every condition of app : Foreground, Background, Removed etc...
firestoreDb!!.collection("example").document("example").collection("real_time_request")
.addSnapshotListener { documentSnapshot: QuerySnapshot?, _: FirebaseFirestoreException? ->
}
I am using service like this to get real time data from Firestore Database when something changes in user's collection.
The ways i tries services are :
FirestoreListeners : IntentService("Firestore Listeners")
FirestoreListeners : Service
FirestoreListeners : JobIntentService
Everything above is working fine when app is in foreground or in background, but the services are killed after app is force closed (when removed from task)
I have tried to make this changes in manifest :
android:stopWithTask="false"
android:directBootAware="true"
android:process=":remote"
In application hierarchy :
android:persistent="true"
In IntentService onHandleIntent :
setIntentRedelivery(true)
In service onStartCommand :
return START_STICKY
Tried to to restart service if the system destroy or kill it :
override fun onTaskRemoved(rootIntent: Intent) {
val restartServiceIntent = Intent(applicationContext, this::class.java)
restartServiceIntent.setPackage(packageName)
val restartServicePendingIntent = PendingIntent.getService(applicationContext, 1, restartServiceIntent, PendingIntent.FLAG_ONE_SHOT)
val alarmService = applicationContext.getSystemService(Context.ALARM_SERVICE) as AlarmManager
alarmService.set(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime() + 1000, restartServicePendingIntent)
Log.e("Service Firestore ", "Task Removed")
super.onTaskRemoved(rootIntent)
}
But nothing works OK. What is the best solution to achieve this ? So the purpose is for something to be running on background and listening for Firestore Changes(or something else) even after app killed or removed from tasks , like Viber etc... Maybe they are using foreground service without notification icon ? But i don't think that Android allow us to make this kind of foreground service (without notification)
I have read some articles about WorkManager
and as Google says :
Note: WorkManager is intended for tasks that require a guarantee that the system will run them even if the app exits, like uploading app data to a server. It is not intended for in-process background work that can safely be terminated if the app process goes away; for situations like that, we recommend using ThreadPools.
But can't figure out how it is exactly work or how to use for my purpose
Thanks
If your Service is started by your app then actually your service is running on main process. so when app is killed service will also be stopped.
IntentService is a base class for Services that handle asynchronous requests (expressed as Intents) on demand. Clients send requests through startService(Intent) calls; the service is started as needed, handles each Intent, in turn, using a worker thread, and stops itself when it runs out of work.
Service is a base class of service implementation. Service runs on the application's main thread which may reduce the application performance. Thus, IntentService , which is a direct subclass of Service is available to make things easier. The IntentService is used to perform a certain task in the background.
You could use FCM push notification to woke up the device when your app is in not running/foreground.When app receives push notifications, you could start your service to do required task. Again you might not be able to start service when app is in background, for that you might need to run service as foreground service.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With