9

I have an app that is managing audio calls. When a call is made to the add and the app is running in the background I need to bring the app in the foreground state. I tried to use Navigator. push but without any result.

Samet ÖZTOPRAK
  • 3,112
  • 3
  • 32
  • 33
Pricope Cosmin
  • 121
  • 1
  • 6

3 Answers3

4

You can use the package bringtoforeground. It's fairly in the early stages with respect to its version but it works.

iOS

But this only works on android, you have to keep in mind that iOS apps that are on the background are destroyed. you can read this do see details here

Android

So this implementation will only work on Android.

The best thing with this package is that you can use it with Firebase Cloud Messaging (FCM) or any other for that matter.

This is their example, Bringtoforeground.bringAppToForeground(); this is the piece of code you use to bring your app to the foreground.

import 'dart:async';

import 'package:bringtoforeground/bringtoforeground.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

void main() => runApp(MyApp());

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  String _platformVersion = 'Unknown';

  @override
  void initState() {
    super.initState();
    initPlatformState();
  }

  // Platform messages are asynchronous, so we initialize in an async method.
  Future<void> initPlatformState() async {
    String platformVersion;
    // Platform messages may fail, so we use a try/catch PlatformException.
    try {
      Timer.periodic(Duration(seconds: 10), (t) {
        Bringtoforeground.bringAppToForeground(); //This is the only thing that matters
      });
    } on PlatformException {
      platformVersion = 'Failed to get platform version.';
    }

    // If the widget was removed from the tree while the asynchronous platform
    // message was in flight, we want to discard the reply rather than calling
    // setState to update our non-existent appearance.
    if (!mounted) return;

    setState(() {
      _platformVersion = platformVersion;
    });
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Plugin example app'),
        ),
        body: Center(
          child: Text('Running on: $_platformVersion\n'),
        ),
      ),
    );
  }
}
Abel Tilahun
  • 1,705
  • 1
  • 16
  • 25
4

Install flutter_foreground_task package here is

and use FlutterForegroundTask.minimizeApp() for app to background
and use FlutterForegroundTask.launchApp() for app to foreground that's all. I think it helps.

Alann Maulana
  • 1,170
  • 12
  • 15
0

if you work with flutter_local_notifications package you can add this argument to AndroidNotificationAction

from here

hope this help (: