How to bring an app from background to foreground

Issue

I want to bring an app in the background to the foreground. This link use a package that is not working and although the author has done a tremendous job it has seen no updates for 11 months.

So I am looking for a solution to bring the app in the background to the foreground or even (re)launch the app. I have spent countless hours trying different packages and none of them work, here is the list:

import 'package:bringtoforeground/bringtoforeground.dart';
import 'package:android_intent/android_intent.dart';
import 'package:flutter_appavailability/flutter_appavailability.dart';
import 'flutterIntent.dart';

flutterIntent.dart is thanks to this repository: https://github.com/sunsetjesus/flutter_intent

import 'dart:async';

import 'package:flutter/services.dart';

class FlutterIntent {
  static const MethodChannel _channel = const MethodChannel('flutter_intent');

  static Future<String> get platformVersion async {
    final String version = await _channel.invokeMethod('getPlatformVersion');
    return version;
  }

  static Future<void> openApp(String appId, String targetActivity) async {
    await _channel.invokeMapMethod('openApp', {"appId": appId,"targetActivity": targetActivity});
  }
}

Not to mention the last of solution for IOS.

I am quite astonished that there are so many good packages but there seems to be no default solution to open an app (which we can do by tapping twice on the icon…). It is very simple to start another app when coding for Windows or Linux platform so I am really amazed that it requires so much effort when it comes to mobile.

Thank you very much for any help,

PS: Actually I don’t even need to bring the app back to the foreground, originally I was trying to have await launch(url); on notification received, so launching chrome/firefox/safari with the url (that I get with no problem) would be good enough in my usecase.

Solution

I solved it myself thanks to flutter local notification package, example below:

import 'package:flutter_local_notifications/flutter_local_notifications.dart';

Future _showNotification(FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin) async {
  var androidPlatformChannelSpecifics = new AndroidNotificationDetails(
      'your channel id', 'your channel name', 'your channel description',
      importance: Importance.max, priority: Priority.high);
  var iOSPlatformChannelSpecifics = new IOSNotificationDetails();
  var platformChannelSpecifics = new NotificationDetails(
      android: androidPlatformChannelSpecifics, iOS: iOSPlatformChannelSpecifics);
  await flutterLocalNotificationsPlugin.show(
    0,
    'New Notification',
    'Flutter is awesome',
    platformChannelSpecifics,
    payload: 'This is notification detail Text...',
  );
}

 static FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin;
  static onBackgroundMessage(SmsMessage message) async {
    String encodedUrl = message.body;
    debugPrint("onBackgroundMessage called $encodedUrl");
    print('app available');
    await _showNotification(flutterLocalNotificationsPlugin);

  }



  Future onSelectNotification(String payload) async {
    showDialog(
      context: context,
      builder: (_) {
        return new AlertDialog(
          title: Text("Your Notification Detail"),
          content: Text("Payload : $payload"),
        );
      },
    );
  }

void localNotification() {
    var initializationSettingsAndroid =
    new AndroidInitializationSettings('app_icon');
    var initializationSettingsIOS = new IOSInitializationSettings();
    var initializationSettings = new InitializationSettings(android: initializationSettingsAndroid, iOS: initializationSettingsIOS);
    flutterLocalNotificationsPlugin = new FlutterLocalNotificationsPlugin();
    flutterLocalNotificationsPlugin.initialize(initializationSettings,
        onSelectNotification: onSelectNotification);

}

And in StatefulWidget:

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

Answered By – Antonin GAVREL

Answer Checked By – Willingham (FlutterFixes Volunteer)

Leave a Reply

Your email address will not be published.