Giter Club home page Giter Club logo

flutter_background's Introduction

flutter_background

This plugin lets you send messages to the Dart side from background processes which runs on platform side. For example, send messages from Android BroadcastReceiver, Worker or Services or IOS background fetch or any other native code which runs in background back to Dart side while app closed.

If your Flutter app contains platform logic such as Android Service ActivityRecognition (Walking, Running, Vehicle etc'), battery status, WorkerManager or IOS BackgroundFetch, Silent Push or any processes that runs when app closed and doesn't use third party plugins, you can use this plugin to send messages from these processes back to dart code in background.

How to use this plugin

Define a background callback with single parameter of type Map<String, dynamic> in your app , (must be a top level function) Insure to mark it with @pragma('vm:entry-point')

@pragma('vm:entry-point')
Future<void> appBackgroundCallback(Map<String, dynamic> params) async {
  print("[appBackgroundCallback]: received background event: $params");

}

Register your background callback on main.dart

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await FlutterBackground.instance.registerBackgroundCallback(appBackgroundCallback);
  runApp(const MyApp());
}

In your native process send events to dart in format of Map<String, Any> in Kotlin or [String: Any?] on Swift

Android Boot Complete example:

class BootCompleteReceiver : BroadcastReceiver(){
    override fun onReceive(c: Context, intent: Intent) {
        FlutterBackgroundPlugin.emitBackgroundEvent(mapOf("action" to "BootComplete"))
    }
}

IOS Background Fetch:

 BGTaskScheduler.shared.register(forTaskWithIdentifier: "com.example.background_fetch_identifier", using: nil) { task in
            self.handleAppRefresh(task: task as! BGAppRefreshTask)
        }

 let request = BGAppRefreshTaskRequest(identifier: "com.example.background_fetch_identifier")
 BGTaskScheduler.shared.submit(request)

 private func handleAppRefresh(task: BGAppRefreshTask) {
        var event : [String: Any?] = [:]
        event["BackgroundFetchComplete"] = task.identifier

        //Send event to Dart side
        FlutterBackgroundPlugin.emitBackgroundEvent(event: event)
    }

flutter_background's People

Contributors

motibartovintenthq avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.