24

What I have studied on stackoverflow and Android documentation. Finally I've concluded this:

There is no way to create a background service for continuous tasks. If I really want a service I should start a foreground service and user continuously sees a persistent notification "App is running". There is no way to hide this notification. It is intentionally added by Google. Yes there are other options like WorkManager and JobScheduler but they do work periodically not continuously.

What I do want is to build an instant messaging app which continuously connects to the server using xmpp or sockets. But it requires a continuous connection but I don’t want to use a foreground service because it shows an irritating notification to the user "App is running".

Question 1: How does Whatsapp and other instant messaging app continuously connect to the server but not show a persistent notification ? How do they achieve this ?

Question 2: If Whatsapp use FCM for notifications then it will also work in those mobile which do not have playservices installed, so how does Whatsapp notification mechanism works ?

Greggz
  • 1,873
  • 1
  • 12
  • 31
Qasim Ahmed
  • 277
  • 2
  • 9
  • https://stackoverflow.com/questions/51289236/continually-running-background-service – AskNilesh Sep 11 '18 at 12:41
  • 1
    Check this [Link 1](https://fabcirablog.weebly.com/blog/creating-a-never-ending-background-service-in-android) [Link 2](https://android.stackexchange.com/a/140782) – Sniffer Sep 11 '18 at 12:45
  • 1
    yes this link doesnot tells how whatsapp work and how to create a service without notification. – Qasim Ahmed Sep 11 '18 at 12:47
  • 5
    if you already know it is not possible, use a different title. Current title is misleading – Tim Sep 11 '18 at 12:49
  • As far as I know Whatsapp and facebook uses Voip services. that are always running in the background – Ahmad Ayyaz Sep 11 '18 at 12:49
  • AFAIK whatsapp does not create infinite long service. It is XMPP based connection and whenever there is new message based on push notification it recreate the xmpp connection on the background thread – Shubham AgaRwal Sep 11 '18 at 12:49
  • 1
    Yes @Killer so on notification received it statrts a job to reconnect xmpp connection ? – Qasim Ahmed Sep 11 '18 at 12:53
  • 1
    @AhmadAyyaz whatsapp uses xmpp connection and how its work in background with out persistent notification in android 8.0 and above – Qasim Ahmed Sep 11 '18 at 12:56
  • 2
    Yes may be they are using some other kind of notification that work without play services like how evernote works without play services but firebase job dispatcher do – Shubham AgaRwal Sep 11 '18 at 12:57
  • 1
    For this purpose I am running service in foreground state and to avoid notifications, I needed to give that notification minimum priority so that notification can be hidden. – Ahmad Ayyaz Sep 11 '18 at 13:02
  • @AhmadAyyaz thanks I will do this to achieve the results. – Qasim Ahmed Sep 11 '18 at 13:03
  • 1
    Also I have a alarm that is always running after every 10 mins to check if service is not running then restart it. – Ahmad Ayyaz Sep 11 '18 at 13:04
  • 1
    https://stackoverflow.com/questions/49137842/fcm-notifications-not-getting-called-in-background-for-oreo – Muhammad Mustafa Sep 12 '18 at 05:00

1 Answers1

9

Starting with Android 6.0 (API level 23), Android introduces two power-saving features that extend battery life for users: DOZE and APP STANDBY. These two features enforce many restrictions on your background processing while the phone is in Doze mode. You should read about Doze and app standby in the following link

https://developer.android.com/training/monitoring-device-state/doze-standby

Now, about your use case is that you want to receive the messages and incoming calls even when the app is not running. For this use case, Android announced High Priority FCM messages in GoogleIO2016. They are high priority Push message which grant the application temporary wakelock and network access, independent of Device's Doze state or if the app happens to be in the app standby. This allows the application to react to the message and notify the user in whatever way it wants about the instant message or incoming call.

I don't know exactly how WhatsApp does that unless I look at their code but you can handle your use case using FCM High Priority Messages. For more about your use case, follow the below link ofGoogleIO2016 Video from 08:30m to 10:30m

https://www.youtube.com/watch?v=VC2Hlb22mZM&t=505s

and read about this use case on the first link in this answer.

Abhishek Luthra
  • 2,575
  • 1
  • 18
  • 34