Skip to content

Commit

Permalink
fix: android build error
Browse files Browse the repository at this point in the history
  • Loading branch information
ekasetiawans committed Nov 30, 2023
1 parent 589c403 commit 8102e01
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 24 deletions.
43 changes: 26 additions & 17 deletions packages/flutter_background_service/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,28 +11,15 @@ A flutter plugin for execute dart code in background.
> **WARNING**:
>
> Please make sure your project already use the version of gradle tools below:
> - in android/build.gradle ```classpath 'com.android.tools.build:gradle:7.1.2'```
> - in android/gradle/wrapper/gradle-wrapper.properties ```distributionUrl=https\://services.gradle.org/distributions/gradle-7.4-all.zip```
> - in android/build.gradle ```classpath 'com.android.tools.build:gradle:7.4.2'```
> - in android/build.gradle ```ext.kotlin_version = '1.8.10'```
> - in android/gradle/wrapper/gradle-wrapper.properties ```distributionUrl=https\://services.gradle.org/distributions/gradle-7.5-all.zip```
### Configuration required for Foreground Services on Android 14 (SDK 34)

Applications that target SDK 34 and use foreground services need to include some additional configuration to declare the type of foreground service they use:

* Determine the type of foreground service your app requires by consulting [the documentation](https://developer.android.com/about/versions/14/changes/fgs-types-required)
* Update your `android/app/build.gradle` file to set the manifest placeholder to the value to use for `android:foregroundServiceType`:

```gradle
android {
...
defaultConfig {
...
// Replace this with the value to use for android:foregroundServiceType
// eg 'camera', 'connectedDevice', 'location', etc
manifestPlaceholders['foregroundServiceType'] = '...'
}
}
```

* Add the corresponding permission to your `android/app/src/main/AndroidManifest.xml` file:

Expand All @@ -45,11 +32,33 @@ android {
Eg, if you picked 'location', use 'android.permission.FOREGROUND_SERVICE_LOCATION'
-->
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_..." />
<application
android:label="example"
android:name="${applicationName}"
android:icon="@mipmap/ic_launcher"
...>

<activity
android:name=".MainActivity"
android:exported="true"
...>

<!--Add this-->
<service
android:name="id.flutter.flutter_background_service.BackgroundService"
android:foregroundServiceType="WhatForegroundServiceTypeDoYouWant"
/>
<!--end-->

...
...
</application>
</manifest>
```

* Consult the documentation to determine if there are runtime permissions you need to request before you can start the service
> **WARNING**:
> * YOU MUST MAKE SURE ANY REQUIRED PERMISSIONS TO BE GRANTED BEFORE YOU START THE SERVICE

### Using custom notification for Foreground Service
You can make your own custom notification for foreground service. It can give you more power to make notifications more attractive to users, for example adding progressbars, buttons, actions, etc. The example below is using [flutter_local_notifications](https://pub.dev/packages/flutter_local_notifications) plugin, but you can use any other notification plugin. You can follow how to make it below:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ apply plugin: 'kotlin-android'
apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle"

android {
compileSdkVersion 33
compileSdkVersion 34

// compileOptions {
// sourceCompatibility JavaVersion.VERSION_1_8
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,19 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="id.flutter.example">

<uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_LOCATION"/>
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_DATA_SYNC"/>
<application
android:label="example"
android:name="${applicationName}"
android:icon="@mipmap/ic_launcher">

<service
android:name="id.flutter.flutter_background_service.BackgroundService"
android:foregroundServiceType="location"
/>

<activity
android:name=".MainActivity"
android:exported="true"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
buildscript {
ext.kotlin_version = '1.6.10'
ext.kotlin_version = '1.8.10'
repositories {
google()
mavenCentral()
}

dependencies {
classpath 'com.android.tools.build:gradle:7.1.2'
classpath 'com.android.tools.build:gradle:7.4.2'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-7.4-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-7.5-all.zip
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
android:exported="true"
android:name=".BackgroundService"
android:stopWithTask="false"
android:foregroundServiceType="${foregroundServiceType}"
/>

<receiver
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ protected void updateNotificationInfo() {
Intent i = getPackageManager().getLaunchIntentForPackage(packageName);

int flags = PendingIntent.FLAG_CANCEL_CURRENT;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
if (SDK_INT >= Build.VERSION_CODES.S) {
flags |= PendingIntent.FLAG_MUTABLE;
}

Expand All @@ -169,7 +169,7 @@ protected void updateNotificationInfo() {
.setContentIntent(pi);

try {
ServiceCompat.startForeground(this, notificationId, mBuilder.build(), ServiceInfo.FOREGROUND_SERVICE_TYPE_MANIFEST);
ServiceCompat.startForeground(this, notificationId, mBuilder.build(), ServiceInfo.FOREGROUND_SERVICE_TYPE_MANIFEST);
} catch (SecurityException e) {
Log.w(TAG, "Failed to start foreground service due to SecurityException - have you forgotten to request a permission? - " + e.getMessage());
}
Expand Down

0 comments on commit 8102e01

Please sign in to comment.