How does apps like Whatsapp or telegram listen to the incoming call/message events on Android?

≡放荡痞女 提交于 2019-12-11 07:05:40

问题


I built a VoIP calling app which maintains a persistent connection with the server to listen to any incoming calls. I implemented a background service to do this.

But since Oreo, this running code is now broken because of the introduction of Background Execution Limits

After looking into forums, I found that some people are suggesting

  1. Convert Service to JobService and let android schedule it

    Doing so, my app won't be able to receive calls when it is stopped

  2. Run your operations in foreground services

    It is annoying for some users to see a constant notification in the notification bar. So these above-mentioned options aren't working for me to fix my code for Oreo.

How does WhatsApp get the incoming (VOIP) call in Android (Oreo onwards) working around the Background Execution Limits?


回答1:


I am assuming that you are using SIP to establish the connection and initiate calls. Without a service constantly re-sending REGISTERs, the app doesn't receive INVITEs when the server sends them.

A workaround for this problem is what is called the "push notification strategy". It works as follows, when the server sends a INVITE, it also sends an FCM notification to your app, This wakes up your app which then sends a REGISTER to your server, which in return forks the call to your app. Here is a video that better explains this strategy




回答2:


There are two options:

  • use platform push services (APNS or FCM)
  • maintain persistent socket connection and exclude application from battery optimisations.



回答3:


(Sticky) foreground services are not affected by the restrictions. So you could use one those as replacement for background services on Oreo.

But foreground services have two disadvantages: They are less likely killed by the system in order to reclaim resources compared to background services, and hence affects the Android system's self-healing capability. And they require you to display a permanent notification. But Users are able to suppress the notification, somewhat mitigating this disadvantage.



来源:https://stackoverflow.com/questions/57035519/how-does-apps-like-whatsapp-or-telegram-listen-to-the-incoming-call-message-even

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!