Skip to content

Commit dc65f7d

Browse files
authored
Adds option to configure notification channel name. (#1463)
* Adds option to configure notification channel name. * Add play service update to ignore list dependabot
1 parent 82a8e38 commit dc65f7d

File tree

10 files changed

+108
-71
lines changed

10 files changed

+108
-71
lines changed

.github/dependabot.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ updates:
2424
- "mvanbeusekom"
2525
open-pull-requests-limit: 10
2626
ignore:
27+
- dependency-name: "com.google.android.gms:play-services-location"
28+
update-types: ["version-update:semver-major", "version-update:semver-minor", "version-update:semver-patch"]
2729
- dependency-name: "com.android.tools.build:gradle"
2830
update-types: ["version-update:semver-minor", "version-update:semver-patch"]
2931
- dependency-name: "junit:junit"

geolocator_android/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
## 4.5.3
2+
3+
* Adds the `notificationChannelName` field to the `ForegroundNotificationConfig` class, allowing to override the channel name.
4+
* Reverts `com.google.android.gms:play-services-location` to version `21.0.1`. Version `21.1.0` forces users to update AGP to version 8.0.0 and Kotlin to 1.9.0 which is not inline with stable Flutter version.
5+
16
## 4.5.2
27

38
* Fixes a bug where the Android `activity` is leaked when the `activiy` is detached from the Flutter application.

geolocator_android/android/build.gradle

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ android {
2626
namespace("com.baseflow.geolocator")
2727
}
2828

29-
compileSdkVersion 33
29+
compileSdk 34
3030

3131
defaultConfig {
3232
minSdkVersion 16
@@ -41,8 +41,8 @@ android {
4141
}
4242

4343
dependencies {
44-
implementation 'com.google.android.gms:play-services-location:21.1.0'
45-
implementation 'androidx.core:core:1.9.0'
44+
implementation 'com.google.android.gms:play-services-location:21.0.1'
45+
implementation 'androidx.core:core:1.12.0'
4646

4747
testImplementation 'junit:junit:4.13.2'
4848
testImplementation 'org.mockito:mockito-core:5.1.1'

geolocator_android/android/src/main/java/com/baseflow/geolocator/GeolocatorLocationService.java

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,8 @@ public void onDestroy() {
8585
}
8686

8787
public boolean canStopLocationService(boolean cancellationRequested) {
88-
if(cancellationRequested) {
89-
return listenerCount == 1;
88+
if (cancellationRequested) {
89+
return listenerCount == 1;
9090
}
9191
return connectedEngines == 0;
9292
}
@@ -143,7 +143,7 @@ public void enableBackgroundMode(ForegroundNotificationOptions options) {
143143
backgroundNotification =
144144
new BackgroundNotification(
145145
this.getApplicationContext(), CHANNEL_ID, ONGOING_NOTIFICATION_ID, options);
146-
backgroundNotification.updateChannel("Background Location");
146+
backgroundNotification.updateChannel(options.getNotificationChannelName());
147147
Notification notification = backgroundNotification.build();
148148
startForeground(ONGOING_NOTIFICATION_ID, notification);
149149
isForeground = true;
@@ -204,13 +204,21 @@ private void obtainWakeLocks(ForegroundNotificationOptions options) {
204204
WifiManager wifiManager =
205205
(WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
206206
if (wifiManager != null) {
207-
wifiLock = wifiManager.createWifiLock(WifiManager.WIFI_MODE_FULL_HIGH_PERF, WIFILOCK_TAG);
207+
wifiLock = wifiManager.createWifiLock(getWifiLockType(), WIFILOCK_TAG);
208208
wifiLock.setReferenceCounted(false);
209209
wifiLock.acquire();
210210
}
211211
}
212212
}
213213

214+
@SuppressWarnings("deprecation")
215+
private int getWifiLockType() {
216+
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.Q) {
217+
return WifiManager.WIFI_MODE_FULL_HIGH_PERF;
218+
}
219+
return WifiManager.WIFI_MODE_FULL_LOW_LATENCY;
220+
}
221+
214222
class LocalBinder extends Binder {
215223
private final GeolocatorLocationService locationService;
216224

geolocator_android/android/src/main/java/com/baseflow/geolocator/location/ForegroundNotificationOptions.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ public class ForegroundNotificationOptions {
1313
@NonNull
1414
private final String notificationText;
1515
@NonNull
16+
private final String notificationChannelName;
17+
@NonNull
1618
private final AndroidIconResource notificationIcon;
1719
private final boolean enableWifiLock;
1820
private final boolean enableWakeLock;
@@ -28,6 +30,7 @@ public static ForegroundNotificationOptions parseArguments(@Nullable Map<String
2830

2931
final AndroidIconResource notificationIcon = AndroidIconResource.parseArguments((Map<String, Object>)arguments.get("notificationIcon"));
3032
final String notificationTitle = (String) arguments.get("notificationTitle");
33+
final String notificationChannelName = (String) arguments.get("notificationChannelName");
3134
final String notificationText = (String) arguments.get("notificationText");
3235
final Boolean enableWifiLock = (Boolean) arguments.get("enableWifiLock");
3336
final Boolean enableWakeLock = (Boolean) arguments.get("enableWakeLock");
@@ -42,6 +45,7 @@ public static ForegroundNotificationOptions parseArguments(@Nullable Map<String
4245
return new ForegroundNotificationOptions(
4346
notificationTitle,
4447
notificationText,
48+
notificationChannelName,
4549
notificationIcon,
4650
enableWifiLock,
4751
enableWakeLock,
@@ -52,13 +56,15 @@ public static ForegroundNotificationOptions parseArguments(@Nullable Map<String
5256
private ForegroundNotificationOptions(
5357
@NonNull String notificationTitle,
5458
@NonNull String notificationText,
59+
@NonNull String notificationChannelName,
5560
@NonNull AndroidIconResource notificationIcon,
5661
boolean enableWifiLock,
5762
boolean enableWakeLock,
5863
boolean setOngoing,
5964
@Nullable Integer color) {
6065
this.notificationTitle = notificationTitle;
6166
this.notificationText = notificationText;
67+
this.notificationChannelName = notificationChannelName;
6268
this.notificationIcon = notificationIcon;
6369
this.enableWifiLock = enableWifiLock;
6470
this.enableWakeLock = enableWakeLock;
@@ -76,6 +82,11 @@ public String getNotificationText() {
7682
return notificationText;
7783
}
7884

85+
@NonNull
86+
public String getNotificationChannelName() {
87+
return notificationChannelName;
88+
}
89+
7990
@NonNull
8091
public AndroidIconResource getNotificationIcon() {
8192
return notificationIcon;

geolocator_android/example/android/app/build.gradle

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
plugins {
2+
id "com.android.application"
3+
id "dev.flutter.flutter-gradle-plugin"
4+
}
5+
16
def localProperties = new Properties()
27
def localPropertiesFile = rootProject.file('local.properties')
38
if (localPropertiesFile.exists()) {
@@ -6,11 +11,6 @@ if (localPropertiesFile.exists()) {
611
}
712
}
813

9-
def flutterRoot = localProperties.getProperty('flutter.sdk')
10-
if (flutterRoot == null) {
11-
throw new GradleException("Flutter SDK not found. Define location with flutter.sdk in the local.properties file.")
12-
}
13-
1414
def flutterVersionCode = localProperties.getProperty('flutter.versionCode')
1515
if (flutterVersionCode == null) {
1616
flutterVersionCode = '1'
@@ -27,12 +27,14 @@ project.getTasks().withType(JavaCompile) {
2727
options.compilerArgs.addAll(args)
2828
}
2929

30-
apply plugin: 'com.android.application'
31-
apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle"
32-
3330
android {
3431
namespace 'com.baseflow.geolocator_example'
35-
compileSdkVersion 33
32+
compileSdkVersion flutter.compileSdkVersion
33+
34+
compileOptions {
35+
sourceCompatibility JavaVersion.VERSION_1_8
36+
targetCompatibility JavaVersion.VERSION_1_8
37+
}
3638

3739
lintOptions {
3840
disable 'InvalidPackage'
@@ -41,8 +43,8 @@ android {
4143
defaultConfig {
4244
// TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
4345
applicationId "com.baseflow.geolocator_example"
44-
minSdkVersion 16
45-
targetSdkVersion 30
46+
minSdkVersion flutter.minSdkVersion
47+
targetSdkVersion flutter.targetSdkVersion
4648

4749
versionCode flutterVersionCode.toInteger()
4850
versionName flutterVersionName

geolocator_android/example/android/build.gradle

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,3 @@
1-
buildscript {
2-
repositories {
3-
google()
4-
mavenCentral()
5-
}
6-
7-
dependencies {
8-
classpath 'com.android.tools.build:gradle:7.4.2'
9-
}
10-
}
11-
121
allprojects {
132
repositories {
143
google()
Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,24 @@
1-
include ':app'
1+
pluginManagement {
2+
def flutterSdkPath = {
3+
def properties = new Properties()
4+
file("local.properties").withInputStream { properties.load(it) }
5+
def flutterSdkPath = properties.getProperty("flutter.sdk")
6+
assert flutterSdkPath != null, "flutter.sdk not set in local.properties"
7+
return flutterSdkPath
8+
}()
29

3-
def flutterProjectRoot = rootProject.projectDir.parentFile.toPath()
10+
includeBuild("$flutterSdkPath/packages/flutter_tools/gradle")
411

5-
def plugins = new Properties()
6-
def pluginsFile = new File(flutterProjectRoot.toFile(), '.flutter-plugins')
7-
if (pluginsFile.exists()) {
8-
pluginsFile.withReader('UTF-8') { reader -> plugins.load(reader) }
12+
repositories {
13+
google()
14+
mavenCentral()
15+
gradlePluginPortal()
16+
}
917
}
1018

11-
plugins.each { name, path ->
12-
def pluginDirectory = flutterProjectRoot.resolve(path).resolve('android').toFile()
13-
include ":$name"
14-
project(":$name").projectDir = pluginDirectory
19+
plugins {
20+
id "dev.flutter.flutter-plugin-loader" version "1.0.0"
21+
id "com.android.application" version "7.4.2" apply false
1522
}
23+
24+
include ":app"

geolocator_android/lib/src/types/foreground_settings.dart

Lines changed: 42 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,53 @@ class AndroidResource {
2727

2828
/// Configuration for the foreground notification. When this is provided the location service will run as a foreground service.
2929
class ForegroundNotificationConfig {
30+
/// Creates an Android specific configuration for the [FlutterBackground] plugin.
31+
///
32+
/// [notificationTitle] is the title used for the foreground service
33+
/// notification.
34+
/// [notificationText] is the body used for the foreground service
35+
/// notification.
36+
/// [notificationChannelName] is the name of the notification channel as it is
37+
/// displayed in the system settings.
38+
/// [notificationIcon] must be a drawable resource.
39+
/// E. g. if the icon with name "background_icon" is in the "drawable"
40+
/// resource folder, it should be of value
41+
/// `AndroidResource(name: 'background_icon', defType: 'drawable').
42+
/// [enableWifiLock] indicates wether or not a WifiLock is acquired, when the
43+
/// background execution is started. This allows the application to keep the
44+
/// Wi-Fi radio awake, even when the user has not used the device in a while.
45+
/// [enableWakeLock] indicates wether or not a Wakelock is acquired, when the
46+
/// background execution is started. If this is false then the system can
47+
/// still sleep and all location events will be received at once when the
48+
/// system wakes up again.
49+
/// [setOngoing] indicates wether or not the displayed notification is
50+
/// persistent and the user cannot dismiss it.
51+
/// [color] is the accent color that is applied to the [notificationIcon].
52+
const ForegroundNotificationConfig({
53+
required this.notificationTitle,
54+
required this.notificationText,
55+
this.notificationChannelName = 'Background Location',
56+
this.notificationIcon =
57+
const AndroidResource(name: 'ic_launcher', defType: 'mipmap'),
58+
this.enableWifiLock = false,
59+
this.enableWakeLock = false,
60+
this.setOngoing = false,
61+
this.color,
62+
});
63+
3064
/// The title used for the foreground service notification.
3165
final String notificationTitle;
3266

3367
/// The body used for the foreground service notification.
3468
final String notificationText;
3569

70+
/// The user visible name of the notification channel.
71+
///
72+
/// The notification channel name will be displayed in the system settings.
73+
/// The maximum recommended length is 40 characters, the name might be
74+
/// truncated if it is to long. Default value: "Background Location".
75+
final String notificationChannelName;
76+
3677
/// The resource name of the icon to be used for the foreground notification.
3778
final AndroidResource notificationIcon;
3879

@@ -68,37 +109,6 @@ class ForegroundNotificationConfig {
68109
/// If this color is null, a system default color is used.
69110
final Color? color;
70111

71-
/// Creates an Android specific configuration for the [FlutterBackground] plugin.
72-
///
73-
/// [notificationTitle] is the title used for the foreground service
74-
/// notification.
75-
/// [notificationText] is the body used for the foreground service
76-
/// notification.
77-
/// [notificationIcon] must be a drawable resource.
78-
/// E. g. if the icon with name "background_icon" is in the "drawable"
79-
/// resource folder, it should be of value
80-
/// `AndroidResource(name: 'background_icon', defType: 'drawable').
81-
/// [enableWifiLock] indicates wether or not a WifiLock is acquired, when the
82-
/// background execution is started. This allows the application to keep the
83-
/// Wi-Fi radio awake, even when the user has not used the device in a while.
84-
/// [enableWakeLock] indicates wether or not a Wakelock is acquired, when the
85-
/// background execution is started. If this is false then the system can
86-
/// still sleep and all location events will be received at once when the
87-
/// system wakes up again.
88-
/// [setOngoing] indicates wether or not the displayed notification is
89-
/// persistent and the user cannot dismiss it.
90-
/// [color] is the accent color that is applied to the [notificationIcon].
91-
const ForegroundNotificationConfig({
92-
required this.notificationTitle,
93-
required this.notificationText,
94-
this.notificationIcon =
95-
const AndroidResource(name: 'ic_launcher', defType: 'mipmap'),
96-
this.enableWifiLock = false,
97-
this.enableWakeLock = false,
98-
this.setOngoing = false,
99-
this.color,
100-
});
101-
102112
/// Returns a JSON representation of this class.
103113
Map<String, dynamic> toJson() {
104114
return {
@@ -107,6 +117,7 @@ class ForegroundNotificationConfig {
107117
'notificationTitle': notificationTitle,
108118
'notificationIcon': notificationIcon.toJson(),
109119
'notificationText': notificationText,
120+
'notificationChannelName': notificationChannelName,
110121
'setOngoing': setOngoing,
111122
'color': color?.value,
112123
};

geolocator_android/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ name: geolocator_android
22
description: Geolocation plugin for Flutter. This plugin provides the Android implementation for the geolocator.
33
repository: https://github.com/baseflow/flutter-geolocator/tree/main/geolocator_android
44
issue_tracker: https://github.com/baseflow/flutter-geolocator/issues?q=is%3Aissue+is%3Aopen
5-
version: 4.5.2
5+
version: 4.5.3
66

77
environment:
88
sdk: ">=2.15.0 <4.0.0"

0 commit comments

Comments
 (0)