flutter_background_service 0.1.0-nullsafety.2 copy "flutter_background_service: ^0.1.0-nullsafety.2" to clipboard
flutter_background_service: ^0.1.0-nullsafety.2 copied to clipboard

outdated

A flutter plugin for executing dart code continously even application closed.

flutter_background_service #

A flutter plugin for execute dart code in background.

Android #

  • No additional setting required.
  • To change notification icon, just add drawable icon with name ic_bg_service_small.

iOS #

  • Register plugins (optional)
import UIKit
import Flutter
import flutter_background_service // add this

@UIApplicationMain
@objc class AppDelegate: FlutterAppDelegate {
    /// add this
    static func registerPlugins(with registry: FlutterPluginRegistry) {
        GeneratedPluginRegistrant.register(with: registry)
    }
    
    override func application(
        _ application: UIApplication,
        didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
    ) -> Bool {

        AppDelegate.registerPlugins(with: self)
        SwiftFlutterBackgroundServicePlugin.setPluginRegistrantCallback { registry in
            AppDelegate.registerPlugins(with: registry)
        }
        
        return super.application(application, didFinishLaunchingWithOptions: launchOptions)
    }
}
  • Playing audio continously

Info.plist

...
	<key>UIBackgroundModes</key>
	<array>
		<string>audio</string>
        ...
	</array>
...

Then use audioplayer plugin to play audio.

...
void main() {
  WidgetsFlutterBinding.ensureInitialized();
  FlutterBackgroundService.initialize(onStart);

  runApp(MyApp());
}

void onStart() {
  WidgetsFlutterBinding.ensureInitialized();

  final audioPlayer = AudioPlayer();

  String url =
      "https://www.mediacollege.com/downloads/sound-effects/nature/forest/rainforest-ambient.mp3";

  audioPlayer.onPlayerStateChanged.listen((event) {
    if (event == AudioPlayerState.COMPLETED) {
      audioPlayer.play(url); // repeat
    }
  });

  audioPlayer.play(url);
}
..

Usage #

  • Follow the example.

Warning #

The code will executed in isolated process, you can't share reference between UI and Service. Use sendData and onDataReceived to communicate between service and UI.

1155
likes
0
pub points
98%
popularity

Publisher

verified publishermaseka.dev

A flutter plugin for executing dart code continously even application closed.

Repository (GitHub)
View/report issues

License

unknown (LICENSE)

Dependencies

flutter

More

Packages that depend on flutter_background_service