Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Flutter 3.0 & Android callback not called #336

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ class BackgroundLocatorPlugin
override fun onMethodCall(call: MethodCall, result: Result) {
when (call.method) {
Keys.METHOD_PLUGIN_INITIALIZE_SERVICE -> {
val args: Map<Any, Any> = call.arguments()
val args: Map<Any, Any> = call.arguments()!!

// save callback dispatcher to use it when device reboots
PreferencesManager.saveCallbackDispatcher(context!!, args)
Expand All @@ -221,7 +221,7 @@ class BackgroundLocatorPlugin
result.success(true)
}
Keys.METHOD_PLUGIN_REGISTER_LOCATION_UPDATE -> {
val args: Map<Any, Any> = call.arguments()
val args: Map<Any, Any> = call.arguments()!!

// save setting to use it when device reboots
PreferencesManager.saveSettings(context!!, args)
Expand All @@ -240,7 +240,7 @@ class BackgroundLocatorPlugin
return
}

val args: Map<Any, Any> = call.arguments()
val args: Map<Any, Any> = call.arguments()!!
updateNotificationText(context!!, args)
result.success(true)
}
Expand All @@ -263,16 +263,16 @@ class BackgroundLocatorPlugin
channel?.setMethodCallHandler(plugin)
}

override fun onNewIntent(intent: Intent?): Boolean {
if (intent?.action != Keys.NOTIFICATION_ACTION) {
override fun onNewIntent(intent: Intent): Boolean {
if (intent.action != Keys.NOTIFICATION_ACTION) {
// this is not our notification
return false
}

val notificationCallback = PreferencesManager.getCallbackHandle(activity!!, Keys.NOTIFICATION_CALLBACK_HANDLE_KEY)
if (notificationCallback != null && IsolateHolderService.backgroundEngine != null) {
val backgroundChannel =
MethodChannel(IsolateHolderService.backgroundEngine?.dartExecutor?.binaryMessenger, Keys.BACKGROUND_CHANNEL_ID)
MethodChannel(IsolateHolderService.backgroundEngine!!.dartExecutor.binaryMessenger!!, Keys.BACKGROUND_CHANNEL_ID)
activity?.mainLooper?.let {
Handler(it)
.post {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ internal fun IsolateHolderService.startLocatorService(context: Context) {
}

backgroundChannel =
MethodChannel(IsolateHolderService.backgroundEngine?.dartExecutor?.binaryMessenger,
MethodChannel(IsolateHolderService.backgroundEngine!!.dartExecutor.binaryMessenger,
Keys.BACKGROUND_CHANNEL_ID)
backgroundChannel.setMethodCallHandler(this)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ class IsolateHolderService : MethodChannel.MethodCallHandler, LocationUpdateList

if (backgroundEngine != null) {
val backgroundChannel =
MethodChannel(backgroundEngine?.dartExecutor?.binaryMessenger, Keys.BACKGROUND_CHANNEL_ID)
MethodChannel(backgroundEngine!!.dartExecutor.binaryMessenger!!, Keys.BACKGROUND_CHANNEL_ID)
Handler(context.mainLooper)
.post {
Log.d("plugin", "sendLocationEvent $result")
Expand All @@ -275,4 +275,4 @@ class IsolateHolderService : MethodChannel.MethodCallHandler, LocationUpdateList
}
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class DisposePluggable : Pluggable {

override fun onServiceDispose(context: Context) {
(PreferencesManager.getCallbackHandle(context, Keys.DISPOSE_CALLBACK_HANDLE_KEY))?.let { disposeCallback ->
val backgroundChannel = MethodChannel(IsolateHolderService.backgroundEngine?.dartExecutor?.binaryMessenger,
val backgroundChannel = MethodChannel(IsolateHolderService.backgroundEngine!!.dartExecutor.binaryMessenger!!,
Keys.BACKGROUND_CHANNEL_ID)
Handler(context.mainLooper)
.post {
Expand All @@ -23,4 +23,4 @@ class DisposePluggable : Pluggable {
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class InitPluggable : Pluggable {
if (!isInitCallbackCalled) {
(PreferencesManager.getCallbackHandle(context, Keys.INIT_CALLBACK_HANDLE_KEY))?.let { initCallback ->
val initialDataMap = PreferencesManager.getDataCallback(context, Keys.INIT_DATA_CALLBACK_KEY)
val backgroundChannel = MethodChannel(IsolateHolderService.backgroundEngine?.dartExecutor?.binaryMessenger,
val backgroundChannel = MethodChannel(IsolateHolderService.backgroundEngine!!.dartExecutor.binaryMessenger!!,
Keys.BACKGROUND_CHANNEL_ID)
Handler(context.mainLooper)
.post {
Expand All @@ -38,4 +38,4 @@ class InitPluggable : Pluggable {
fun setInitData(context: Context, data: Map<*, *>) {
PreferencesManager.setDataCallback(context, Keys.INIT_DATA_CALLBACK_KEY, data)
}
}
}
2 changes: 1 addition & 1 deletion example/android/app/build.gradle
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 30
compileSdkVersion 31

sourceSets {
main.java.srcDirs += 'src/main/kotlin'
Expand Down
6 changes: 3 additions & 3 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import 'dart:async';
import 'dart:ffi';
import 'dart:isolate';
import 'dart:ui';

Expand Down Expand Up @@ -203,9 +202,10 @@ class _MyAppState extends State<MyApp> {
}
}

Future<void> _startLocator() async{
Future<void> _startLocator() async {
Map<String, dynamic> data = {'countInit': 1};
return await BackgroundLocator.registerLocationUpdate(LocationCallbackHandler.callback,
return await BackgroundLocator.registerLocationUpdate(
LocationCallbackHandler.callback,
initCallback: LocationCallbackHandler.initCallback,
initDataCallback: data,
disposeCallback: LocationCallbackHandler.disposeCallback,
Expand Down
2 changes: 1 addition & 1 deletion lib/background_locator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class BackgroundLocator {
AndroidSettings androidSettings = const AndroidSettings(),
IOSSettings iosSettings = const IOSSettings()}) async {
if (autoStop) {
WidgetsBinding.instance!.addObserver(AutoStopHandler());
WidgetsBinding.instance.addObserver(AutoStopHandler());
}

final args = SettingsUtil.getArgumentsMap(
Expand Down
2 changes: 1 addition & 1 deletion lib/location_dto.dart
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class LocationDto {
json[Keys.ARG_HEADING],
json[Keys.ARG_TIME],
isLocationMocked,
json[Keys.ARG_PROVIDER],
json[Keys.ARG_PROVIDER] ?? "",
);
}

Expand Down