如何将应用程序从后台带到前台

时间:2021-01-24 19:27:51

标签: android flutter dart notifications dart-isolates

我想将后台的应用程序带到前台。 This link 使用了一个无法正常工作的软件包,尽管作者做了大量工作,但 11 个月没有看到任何更新。

所以我正在寻找一种解决方案,将应用程序从后台带到前台,甚至(重新)启动应用程序。我花了无数个小时尝试不同的软件包,但没有一个起作用,这是列表:

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

flutterIntent.dart 感谢这个仓库: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});
  }
}

更不用说最后的IOS解决方案了。

我很惊讶有这么多好的软件包,但似乎没有打开应用程序的默认解决方案(我们可以通过点击图标两次来实现...)。在为 Windows 或 Linux 平台编码时启动另一个应用程序非常简单,所以我真的很惊讶它在移动方面需要如此多的努力。

非常感谢您的帮助,

PS:实际上我什至不需要将应用程序带回前台,最初我试图在收到通知时使用 await launch(url);,因此使用 url 启动 chrome/firefox/safari(我得到没问题)在我的用例中已经足够了。

1 个答案:

答案 0 :(得分:0)

感谢flutter local notification package,我自己解决了这个问题,示例如下:

Can't run because no spec files were found.

We searched for any files matching this glob pattern:

tests/spec.js

在 StatefulWidget 中:

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);

}