2

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.

Carlton Foster
  • 141
  • 2
  • 7

2 Answers2

1

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.

kuhnroyal
  • 7,188
  • 1
  • 34
  • 47
0

You need to use a couple of things together:

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.

Rod
  • 1,601
  • 12
  • 18
  • Thanks Racr0x for those helpful links. I created a flutter app using WebRTC and websocket for signaling which works fine only when the app is in the foreground. I watched the print messages when the app is paused or inactive and even the navigation (with global key) and ringtone works, but the app remains in the paused or inactive lifecycle state. I will look at the flutter_callkeep package to see if it will wake up the app and move it to resume state. – Carlton Foster Aug 21 '20 at 12:17
  • 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. Tell me if you find any dificulties, I'll be happy to help you. – Rod Aug 21 '20 at 13:02
  • Someone said that in order to bring my app to the foreground, I must call startActivity() from another context (either a Service or a BroadcastReceiver). (https://stackoverflow.com/questions/29766270/how-to-resume-android-activity-programmatically-from-background) – Carlton Foster Aug 21 '20 at 22:04
  • If my activities are on different tasks, He said I can use this to bring the activity's task to foreground: ActivityManager activityManager = (ActivityManager) getApplicationContext().getSystemService(Context.ACTIVITY_SERVICE); activityManager.moveTaskToFront(getTaskId(), ActivityManager.MOVE_TASK_NO_USER_ACTION); But this looks like native code (java or iOS type code). I don't know how to do this in Flutter. Is there a Flutter library or package to achieve this objective? – Carlton Foster Aug 21 '20 at 22:04
  • This is native Java for Android. – Rod Aug 22 '20 at 01:50
  • What you are trying to do is not simple and there is no out of the box solution for it. Because Android and IOS have different ways to handle calls and notifications, you will need to go deep learning the way they work. Many people asked the same thing before and none of the previous questions have answers. Its not that hard, but anyone willing to do it will need to learn many things most people just don't want to spend time learning. – Rod Aug 22 '20 at 01:56
  • Hey @racr0x, I've been trying to solve this issue and I came up with your suggestions. I've managed to call from a background Dart service some Android native code trying to start a new activity with an intent. However, I can see some errors in logcat when doing that. Would you like to chat somewhere about this problem? – pitazzo Sep 30 '20 at 15:22