Flutter: automatically resume app from background service - flutter

Is there a way to automatically resume a flutter app from a paused or inactive lifecycle state with a background service?
Similar to when the WhatsApp app received a call while it was paused or inactive.

No, you can not simply wake up the app from a service. This would open the door for all kinds of spam apps and security risks. You need to specifically register it as VOIP app using CallKit or something like SIP on Android to receive incoming calls.
For other things you can use push notifications but that won't start your app. The user always has to click on the notification that you display.

You need to use a couple of things together:
Use push notifications to wake up your app:
https://pub.dev/packages/firebase_messaging
To start your app using push notifications refers to this post:
https://stackoverflow.com/a/48405551/4335775
Use CallKit (IOS) or ConnectionServices (Android) to show the upcoming call screen. By the day of this answer there are only a few packages to handle these things, here is one that can handle both platforms:
https://pub.dev/packages/flutter_callkeep
If you want a completely different thing and need to run some background process, there are bunch whole of things you should know first. I suggest beginning here: https://flutter.dev/docs/development/packages-and-plugins/background-processes
Here is a usefull package to work with background processes that should be constantly running:
https://pub.dev/packages/background_fetch
The app can only be waked up with explicit commands. The user must hit the button. And there are ways to handle the input, messages may pass parameters, you can save the previous state in the database or shared preferences, etc.

Related

Incoming call notification

We are developing an app with angular/ionic in which we use capacitor jitsi plugin for video calls. What we are now trying to do is to receive notifications (via firebase) like in whatsapp with the incoming call screen and two buttons to accept and decline. Any idea on how to do that?
Thanks
If you got the choice to change the notification service, instead or directly using firebase, you could use Onesignal which extends firebase and they already have a service named VOIP notifications which should kinda do your needs and here is the link:
https://documentation.onesignal.com/docs/voip-notifications
In case your are restricted with firebase or need to know how this could be done, bellow will be the way to achieve it..
As for android:
First as logic part, you need to add some code in the native layer since hybrid apps usually can't interact from JavaScript side to native side in case app was not launched, so in order to wake the application on a specific event like notification received or any other actions that phone system can hold..
Second, as technical part, you need to add broadcast receivers and the receivers role stand to interact as native code with system. example in the link below:
https://www.digitalocean.com/community/tutorials/android-broadcastreceiver-example-tutorial
also another video about foreground and background broadcast receiver service in the link below:
https://www.youtube.com/watch?v=rlzfcqDlovg
video code output in git:
https://github.com/borntocoderepos/callrecorder
in the Youtube video example, the user is launching a toast message on phone call if app was opened or closed (background or foreground) so you can launch your app with intent with passing data and capture the data on app start as Deep Links as capacitor (https://capacitorjs.com/docs/guides/deep-links) or Cordova (https://ionicframework.com/docs/native/deeplinks)..
And instead of listening to network or phone calls, you can listen to Notifications and for sure you need to do searches about your topic and or the notification service that you'll choose.
Now for the video and the tutorial not sure of the quality of code so make sure to do more researches about the way its done from different places (could be outdated code or bad code quality or even not complete service and will discuss about this point below).
In android there is policy about using background and foreground services so once you start a service you need to end it after your done so make sure after you receive the notification and launch your app to stop the listening since it would cost power usage and perhaps could be stopped by Playstore as harmful app.
Now considering IOS it should be the same concept so make searches about this topic, but for IOS, the listeners policy as I remember , the receivers should not be waked up for more than 15 mins, so also keep this in mind and make sure you stop the receivers directly after launching your Hybrid app.
Broadcast equivalent receiver for IOS:
http://www.andrewcbancroft.com/2014/10/08/fundamentals-of-nsnotificationcenter-in-swift/

how to open calling screen when App is killed , I am using agora.io

I am using agora_rtc_engine for video calling.
I have tested the Firebase background message but the documentation mentions:
Since the handler runs in its own isolate outside your applications
context, it is not possible to update application state or execute any
UI impacting logic. You can however perform logic such as HTTP
requests, IO operations (updating local storage), communicate with
other plugins etc.
Source https://firebase.flutter.dev/docs/messaging/usage/
How I can display incoming call screen when Firebase notification arrived in the background when App is killed?
I also use callkeep. It shows phone's default calling screen but when I click on answer call it starts the call. I am not able to navigate to specific screen.
Thanks.
Here is something that you can try - using the CallKeepPerformAnswerCallAction event you can bring your app to foreground using bringtoforeground

How to open an app automatically when app notification appears with Swift

I want an app that can be opened automatically or by itself when app notification appears. I've been looking for this case but I don't find any solution.
This is not possible on iOS. You can not programmatically open an app based on a notification.
What you can do is to use silent pushes (those are push notifications that are invisible to the user) to wake up your application in the background. Using this mechanism, you can do some work in your app in the background (like fetching some data so it is instantly available later on).
Depending on your use case, this might be of use to you.

IOS background work

I have an iPhone application like facebook for iPhone. My application must connect my server and read all message every two hours regularly. I have a thread to read all message but when the application is terminated the thread cannot work. Can the thread run undependently from main delegate or how can I find solution for this problem?
You cannot have your app do stuff in the background. There is an API to finish tasks like uploading a photo but even that will be killed after around 10 minutes.
But the Apple Push Notification Service seems like the most appropriate solution for your problem. Your server notifies the device that there is something new happening and you fetch the actual messages when the user opens the app.
edit: As of iOS 7 Apple implemented a feature where you can schedule running tasks to fetch data in the background. Those tasks are not guaranteed to run at any specific times. See the release notes for iOS 7 and the linked methods below:
Apps that regularly update their content by contacting a server can
register with the system and be launched periodically to retrieve that
content in the background. To register, include the UIBackgroundModes
key with the fetch value in your app’s Info.plist file. Then, when
your app is launched, call the setMinimumBackgroundFetchInterval:
method to determine how often it receives update messages. Finally,
you must also implement the
application:performFetchWithCompletionHandler: method in your app
delegate.
There is no solution.
Apple does not permit applications to run in the background unless they are of a specific type such as location or audio or voip or newstand (your app can continue to run for about 10 minutes after it was active if it uses shouldBeginBackgroundTaskWithExpirationHandler).
There is no workaround, many many other people have wondered how to do the same thing as yourself before, but there is no legitimate way. Your app cannot schedule any sort of periodic call home activity.
The only way your app can run once its gone into a suspended or terminated state is for the user to launch it, either explicitly or in reponse to a local notification or remote push notification.

iPhone - how to be notified of call completion

I'm developing an application that needs to take action on completed phone calls, preferably right after the call ends but minimally once per day.
I've read up on the new CoreTelphony framework, and it seems I can get call events if my app is active, but I don't see how to launch/wake my app when a call ends if my app is not the foreground app. I also don't see how any of the new pseudo-background "modes" would allow my app to listen for these events in the background. Do any of you know how this might be done?
If post-call processing isn't possible, then I'd like to figure out a way to automatically wake my app up once per day, pull all of the call events since the last wakeup, and process them. I know how I might do this with Push or Local notifications, but my understanding is that those require user action to continue; in this case, I just want the processing to happen automatically. Is there a mechanism that would enable this?
Thanks,
Dan
You can't launch your app without user interaction.
Push / Local notifications aren't for this kind of thing, they're for letting the user know about event.
On a non-jailbroken device there is no way to do what you want to do.