简体   繁体   English

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

[英]How to bring an app from background to foreground

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. 此链接使用 package 无法正常工作,尽管作者做了出色的工作,但 11 个月没有更新。

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

Not to mention the last of solution for IOS.更不用说 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.在为 Windows 或 Linux 平台编码时,启动另一个应用程序非常简单,所以我真的很惊讶它在移动方面需要这么多努力。

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); PS:实际上我什至不需要将应用程序带回前台,最初我试图让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.收到通知后,因此在我的用例中使用 url(我没有问题)启动 chrome/firefox/safari 就足够了。

I solved it myself thanks to flutter local notification package , example below:感谢flutter 本地通知 package ,我自己解决了这个问题,示例如下:

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:在 StatefulWidget 中:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从 ForegroundService 中将应用程序带到前台 - How to bring app to foreground from within ForegroundService Phonegap - 从背景到前景 - Phonegap - Bring from background to foreground 电话通话结束后,将拨号器应用程序从后台置于前台 - Bring dialer app into foreground from background when phone call is finished 如何将监视光传感器的背景安卓应用程序带到前景 - How to bring background android app which monitors light sensor to foreground 如何将Android应用程序在后台运行到前台 - Ways to bring an Android app in background to foreground 将应用程序带到后台然后到前台 - Bring application to background and then to foreground 如何检查活动是否为前台,并在活动为前台时将其带入后台? - How to check whether a activity is foreground and bring it to background when it is foreground? 如何将Android应用程序从前台传递到后台 - How to pass Android app from foreground to background 如何在颤振集成测试中将应用程序带到前台 - How to bring app to foreground in flutter integration test 如何在Calabash-android中将应用程序带到Foreground? - How to bring app to Foreground in calabash-android?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM