即使关闭了应用程序,如何运行代码

发布于 2025-01-21 14:49:21 字数 80 浏览 0 评论 0 原文

即使关闭该应用程序,Flutter应用程序仍然可以运行代码吗?我正在尝试构建一个通知用户的应用程序,即使该应用程序已关闭。希望得到答案。 tia。

Can an flutter app still run code even if the app is closed? I am trying to build an application that notifies a user even if the app is closed. Hoping for answers. TIA.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

初吻给了烟 2025-01-28 14:49:22

默认情况下,您必须以特定于平台的方式集成背景服务。

但是我发现此软件包主要为您处理本机集成: flutter_background_service

final service = FlutterBackgroundService();
  await service.configure(
    androidConfiguration: AndroidConfiguration(
      // this will executed when app is in foreground or background in separated isolate
      onStart: onStart,

      // auto start service
      autoStart: true,
      isForegroundMode: true,
    ),
    iosConfiguration: IosConfiguration(
      // auto start service
      autoStart: true,

      // this will executed when app is in foreground in separated isolate
      onForeground: onStart,

      // you have to enable background fetch capability on xcode project
      onBackground: onIosBackground,
    ),
  );
  service.startService();

代码段从包装示例在这里

如何将软件包添加到您的项目:

  1. 打开 pubspec.yaml 文件
  2. 添加 flutter_background_service: ^2.4.3 to dependenty e节
  3. 运行 flutter Pub Get

By default you would have to integrate your background service on a platform specific way.

But I found this package that handles the native integration mostly for you: flutter_background_service.

final service = FlutterBackgroundService();
  await service.configure(
    androidConfiguration: AndroidConfiguration(
      // this will executed when app is in foreground or background in separated isolate
      onStart: onStart,

      // auto start service
      autoStart: true,
      isForegroundMode: true,
    ),
    iosConfiguration: IosConfiguration(
      // auto start service
      autoStart: true,

      // this will executed when app is in foreground in separated isolate
      onForeground: onStart,

      // you have to enable background fetch capability on xcode project
      onBackground: onIosBackground,
    ),
  );
  service.startService();

Code snippet taken from the package example here.

How to add the package to your project:

  1. Open your pubspec.yaml file
  2. Add flutter_background_service: ^2.4.3 to your dependency section
  3. Run flutter pub get
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文