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

Listen to incoming messages when app is in background or closed #115

Closed rbluethl closed 2 years ago

rbluethl commented 2 years ago

Hey! I've been intensively digging through all (open & closed) issues regarding listening in background and listening when the app is closed, however I'm still not quite sure if I'm doing everything right.

In #71 it is mentioned, that it should be possible to listen to incoming messages even when the app is closed. Is that really the case? What if I start my phone and do not explicitly open the app, how should it be possible that the app still receives incoming messages?

In #90 you say that "The package does support running when the app is closed. The reason it may not work as expected in most cases is that the device blocks the background execution of the app.". Does that mean it should basically work without any further ado?

From other apps I have seen alerts in the notification center that an app is running in the background (see attached screenshot). Is that even necessary/does Telephony do something differently?

It's not entirely clear to me how reliably listening in the background should be approached. Maybe you could elaborate a bit @shounakmulay ? Do you think that it's always necessary to tweak the battery settings depending on the device/manufacturer? Thank you for being so kind and developing this helpful package!

CleanShot 2021-09-23 at 18 38 14@2x

shounakmulay commented 2 years ago

Hey @rbluethl Thanks for checking out the package. To answer your questions

  1. Yes listening in the background will work even if you don't open the app. The reason this works is we are registering a static broadcast receiver in the app's manifest file. That means the android system can initialize that broadcast receiver independent of the app.

  2. In most cases the device manufacturers and custom android OS environments block most of the app from performing operations in the background. That means your app won't receive any broadcasts and if it tries to start services in the background those will be killed as well. Don't kill my app is a good place to look at what particular device manufacturers are doing in terms of blocking background operations. If you are facing this problem there isn't much we can do as app developers other than asking the user to turn off battery optimization.

  3. The notification that you see from other apps comes from a Foreground Service. Telephony uses a Broadcast Receiver and not a Service. Incoming SMS on android are generally detected using Broadcast Receivers and not Services.

Listening to SMS or doing any background work, in general, comes down to how time-sensitive is the task.

All of these approaches are still vulnerable to the above mentions restrictions from the device manufacturers. Unfortunately, there is no way around it as of yet. If optimizations are turned off for your app or the if the device does not block background tasks aggressively (eg. Google, Motorola devices) these methods are pretty reliable.

Hope this answers all your questions.

rbluethl commented 2 years ago

Hey @shounakmulay! Thank you very much for your comprehensive answer - you're doing a great job at carefully responding to all kinds of issues/questions.

  1. I see, thank you. And is there anything that needs to be explicitly done for the OS to take this step? I'm wondering if my app starts listening to messages every time I start the phone?
  2. Understood, I stumbled upon this site a few days ago already when researching an issue that messages weren't being delivered. It's a bit a of a pain on Android, but I guess there's nothing else we can do here.
  3. Right, thank you for clarifying.

You definitely answered all my questions, thank you again!

shounakmulay commented 2 years ago

Following the docs should be enough. Because we register the receiver statically the app doesn't need to be open after every restart.

github-actions[bot] commented 2 years ago

Issue marked stale due to no activity.

premsingh01 commented 1 year ago

Listening to incoming SMS in background is not working !