패키지, 저장소 및 앱 카테고리 검색

패키지 상세v1.3.1

flutter_background

Android에서 포그라운드 서비스를 사용하여 앱을 백그라운드에서 실행합니다. 백그라운드 데이터 동기화 또는 메시징과 같은 지속적인 작동이 필요한 애플리케이션에 적합합니다.

Android
30일 다운로드
4.7만
좋아요
342
Pub 점수
160

패키지 설치

flutter pub add flutter_background

패키지 개요

공개 정보를 바탕으로 생성한 선택 가이드입니다.

flutter_background 패키지는 Android 앱이 지속적인 서비스를 사용하여 포그라운드에서 실행되도록 하여, 앱이 사용 중이 아닐 때에도 지속적인 작동을 보장합니다. 백그라운드 데이터 동기화, 실시간 메시징 또는 위치 추적과 같이 중단 없이 실행이 필요한 작업에 이상적입니다. Android의 포그라운드 서비스 기능을 활용함으로써 OS가 메모리 관리로 인해 앱을 종료하는 것을 방지합니다. 이 패키지는 Flutter와 원활하게 통합되며 Android 플랫폼만 지원합니다.

사용 사례

  • 백그라운드 데이터 동기화
  • 실시간 메시징
  • 위치 추적
  • 장시간 실행 작업
  • 지속 서비스 실행

주요 기능

  • 포그라운드 서비스 지원
  • Android 전용 플랫폼
  • 지속적인 백그라운드 실행
  • Flutter와의 간편한 통합
  • 앱 종료를 방지합니다

적합한 대상

  • 지속적인 백그라운드 작동이 필요한 앱
  • 메시징 또는 동기화 기능을 구축하는 개발자
  • 신뢰할 수 있는 포그라운드 서비스가 필요한 프로젝트
  • 안드로이드 중심 모바일 애플리케이션
  • 앱 안정성이 중요한 사용 사례

고려 사항

  • Android 전용
  • 포그라운드 서비스를 사용하려면 사용자 권한이 필요합니다
  • 배터리 수명에 영향을 줄 수 있습니다
  • 라이프사이클 이벤트를 올바르게 처리해야 합니다
  • iOS와 호환되지 않습니다

원본 README

연결된 소스 저장소의 README 원문 스냅샷입니다. 최신 내용은 저장소에서 확인하세요.

flutter_background

"Buy Me A Coffee"

A plugin to keep flutter apps running in the background. Currently only works with Android.

It achieves this functionality by running an Android foreground service in combination with a partial wake lock and disabling battery optimizations in order to keep the flutter isolate running.

Note: This plugin currently only works with Android. PRs for iOS are very welcome, although I am not sure if a similar effect can be achieved with iOS at all.

Getting started

To use this plugin, add flutter_background as a dependency in your pubspec.yaml file.

Android

Add the following permissions to the AndroidManifest.xml:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="de.julianassmann.flutter_background_example">

    <!-- Adapt to the foreground service type(s) desired, these are just examples -->
    <uses-permission android:name="android.permission.FOREGROUND_SERVICE_DATA_SYNC" />
    <uses-permission android:name="android.permission.FOREGROUND_SERVICE_SPECIAL_USE" />

    <application>
        ...

        <!-- Adapt to the foreground service type(s) desired, these are just examples -->
        <service
            android:name="de.julianassmann.flutter_background.IsolateHolderService"
            android:exported="false"
            android:foregroundServiceType="dataSync|specialUse|..." />
    </application>
</manifest>

Important: You must specify the appropriate foregroundServiceType for your use case. See the Android docs and the list of available service typs for more information on foreground service types.

Other platforms

Android is the only supported platform. There are currently no plans to extend support for other platforms, but feel free to change that by contributing to this plugin.

Usage

Import flutter_background.dart:

import 'package:flutter_background/flutter_background.dart';
Initializing plugin and handling permissions

Before you can use this plugin, you need to initialize it by calling FlutterBackground.initialize(...):

final androidConfig = FlutterBackgroundAndroidConfig(
    notificationTitle: "flutter_background example app",
    notificationText: "Background notification for keeping the example app running in the background",
    notificationImportance: AndroidNotificationImportance.normal,
    notificationIcon: AndroidResource(name: 'background_icon', defType: 'drawable'), // Default is ic_launcher from folder mipmap
);
bool success = await FlutterBackground.initialize(androidConfig: androidConfig);

This ensures all permissions are granted and requests them if necessary. It also configures the foreground notification. The configuration above results in the foreground notification shown below when running FlutterBackground.enableBackgroundExecution().

The foreground notification created by the code above.

The arguments are:

  • notificationTitle: The title used for the foreground service notification.
  • notificationText: The body used for the foreground service notification.
  • notificationImportance: The importance of the foreground service notification.
  • notificationIcon: The icon used for the foreground service notification shown in the top left corner. This must be a drawable Android Resource (see here for more). E. g. if the icon with name "background_icon" is in the "drawable" resource folder, it should be of value `AndroidResource(name: 'background_icon', defType: 'drawable').
  • enableWifiLock: Indicates whether or not a WifiLock is acquired when background execution is started. This allows the application to keep the Wi-Fi radio awake, even when the user has not used the device in a while (e.g. for background network communications).

In this example, background_icon is a drawable resource in the drawable folders (see the example app). For more information check out the Android documentation for creating notification icons for more information how to create and store an icon.

In order to function correctly, this plugin needs a few permissions. FlutterBackground.initialize(...) will request permissions from the user if necessary. You can call initialize more than one time, so you can call initalize() every time before you call enableBackgroundExecution() (see below).

In order to notify the user about upcoming permission requests by the system, you need to know, whether or not the app already has these permissions. You can find out by calling

bool hasPermissions = await FlutterBackground.hasPermissions;

before calling FlutterBackground.initialize(...). If the app already has all necessary permissions, no permission requests will be displayed to the user.

Run app in background

With

bool success = await FlutterBackground.enableBackgroundExecution();

you can try to get the app running in the background. You must call FlutterBackground.initialize() before calling FlutterBackground.enableBackgroundExecution().

With

await FlutterBackground.disableBackgroundExecution();

you can stop the background execution of the app. You must call FlutterBackground.initialize() before calling FlutterBackground.disableBackgroundExecution().

To check whether background execution is currently enabled, use

bool enabled = FlutterBackground.isBackgroundExecutionEnabled;

Example

The example is a TCP chat app: It can connect to a TCP server and send and receive messages. The user is notified about incoming messages by notifications created with the plugin flutter_local_notifications.

Using this plugin, the example app can maintain the TCP connection with the server, receiving messages and creating notifications for the user even when in the background.

Maintainer

Julian Aßmann

If you experience any problems with this package, please create an issue on Github. Pull requests are also very welcome.

카테고리

의존성

의존성 목록 1 개

  • plugin_platform_interface^2.1.8

소스 저장소

이 패키지를 사용하는 프로젝트

순위