shounakmulay / Telephony

Flutter plugin for telephony features like query device sms directory, listen for incoming messages, retrieve various network parameters, etc.
https://telephony.shounakmulay.dev
MIT License
138 stars 126 forks source link

Background listening to SMS #148

Open sudipta1411 opened 2 years ago

sudipta1411 commented 2 years ago

I am trying to implement call listen in the background feature in order to learn flutter. My code is based on your project. The code is working when I minimize the app (the app is not in foreground). However when the app is killed, it does not work. My initial guess is that the BroadcastReceiver is not alive when I kill the app. Based on your code I am not sure what I am doing wrong. How are you keeping the IncomingSmsReceiver alive after the app is killed? I have asked https://stackoverflow.com/questions/72402844/issue-running-a-background-task-in-android-when-the-app-is-killed . It would be nice to have your feedback. Do I need to add some permission to the receiver?

This is not an issue per se, but there is not much documentation available on the web. It would be great if you kindly give me some pointer before closing the issue. Thanks.

P.S. I did some more digging. It turns out that when I close my app, the IncomingSmsReceiver runs is a diiferent process, as evident from the following log from adb logcat :

D Zygote  : Forked child process 6287
I ActivityManager: Start proc 6287:org.sudipta.sms_handler_example/u0a525 for broadcast {org.sudipta.sms_handler_example/com.shounakmulay.telephony.sms.IncomingSmsReceiver}

This happens only when I close my app (not when the app is in background). Can you please explain this? Thanks

sudipta1411 commented 2 years ago

Turns out that one needs to disable battery optimization feature in certain mobiles. Now my code is working. But I still do not understand why Telephony is working in the same mobile without disabling battery optimization.