foreground_service 0.2.1

foreground_service #

Create Android foreground service/notification

Prep (Android side): #

Step 1 - Create FlutterApplication subclass #
i.e. (Kotlin)

class OverrideApplication: FlutterApplication(), PluginRegistry.PluginRegistrantCallback{
    override fun onCreate() {
        super.onCreate()
        ForegroundServicePlugin.setPluginRegistrantCallback(this)
    }

    override fun registerWith(p0: PluginRegistry?) {
        GeneratedPluginRegistrant.registerWith(p0)
    }
}
Step 2 - Make necessary changes to android manifest #
Don't delete things willy-nilly unless you know what you're doing.
Just add lines/modify as necessary.

If you're having trouble, take a look at the /example app.

<manifest>
    <!-- add this line -->
    <uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>

    <!-- there should already be an <application/> section -->
    <!-- just modify the value of android:name, leave everything else -->
    <application android:name=".OverrideApplication">

        <!-- add this line within the application section -->
        <service android:name="org.thebus.foreground_service.ForegroundServicePlugin"
            android:exported="false"/>
    </application>
</manifest>
Step 3 #
Add icon resource to project.

The icon needs to be in this specific location:

res/drawable/org_thebus_foregroundserviceplugin_notificationicon

(take a look at the /example app if you're confused)

Use (Flutter/Dart side): #

To start the service, call ForegroundService.startForegroundService([serviceFunction])

serviceFunction will then be executed periodically, but "minimum/best-effort"
i.e. it will try to make the interval between function executions *at least* that long

Doesn't work? #

As long as you're calling ForegroundService.startForegroundService,
"flutter run" should show error messages that indicate what's wrong/missing

i.e. messages beginning with E/ForegroundServicePlugin indicate an error from the plugin

Caution: #

ForegroundService.notification.get* methods may give unexpected values.

Once notifications are sent out, there's no way to retrieve the "current" data.

To work around this, the plugin keeps a version of the notification around.
This version may not have been "sent out" yet, however.

Disclaimer:

Most of the fancy stuff is shamelessly pilfered from the android_alarm_manager plugin

0.2.1 #

Should no longer crash due to @UiThread exception.

0.2.0 #

Add functions to check service started status & stop service

0.1.1+1 #

Slightly updated readme

0.1.1 #

Fix bug (class would try to init Instant regardless of api level)

0.1.0+2 #

Try to fix wording. ("No" got left behind on a separate line, so it look liked it said "iOS support planned" at a glance).

0.1.0+1 #

Typo

0.1.0 #

1st version. Should suffice to take care of the simple case/bare necessities: Change title, Change text, Change icon, Change priority, and most importantly run a dart function within the context of a foreground service.

example/README.md

foreground_service_example #

Demonstrates how to use the foreground_service plugin.

Getting Started #

This project is a starting point for a Flutter application.

A few resources to get you started if this is your first Flutter project:

For help getting started with Flutter, view our online documentation, which offers tutorials, samples, guidance on mobile development, and a full API reference.

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  foreground_service: ^0.2.1

2. Install it

You can install packages from the command line:

with Flutter:


$ flutter pub get

Alternatively, your editor might support flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:foreground_service/foreground_service.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
49
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
74
Learn more about scoring.

We analyzed this package on Jul 15, 2019, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.4.0
  • pana: 0.12.19
  • Flutter: 1.7.8+hotfix.3

Platforms

Detected platforms: Flutter

References Flutter, and has no conflicting libraries.

Health suggestions

Format lib/foreground_service.dart.

Run flutter format to format lib/foreground_service.dart.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.1.0 <3.0.0
flutter 0.0.0
Transitive dependencies
collection 1.14.11
meta 1.1.6 1.1.7
sky_engine 0.0.99
typed_data 1.1.6
vector_math 2.0.8
Dev dependencies
flutter_test

Admin