From d477ce99c33848b4aa7fbdc99812234faafa983a Mon Sep 17 00:00:00 2001 From: Derek Date: Thu, 30 Nov 2023 17:42:19 -0500 Subject: [PATCH 1/7] Update background_android_isolate_platform.dart I made the call to the onPushBackgroundMessage await for the handler to complete. --- lib/src/platform/src/background_android_isolate_platform.dart | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/src/platform/src/background_android_isolate_platform.dart b/lib/src/platform/src/background_android_isolate_platform.dart index cf743b3b5..5563fe0ff 100644 --- a/lib/src/platform/src/background_android_isolate_platform.dart +++ b/lib/src/platform/src/background_android_isolate_platform.dart @@ -20,7 +20,8 @@ class BackgroundIsolateAndroidPlatform { _methodChannel.setMethodCallHandler((call) async { switch (call.method) { case PlatformMethod.pushOnBackgroundMessage: - return _onPushBackgroundMessage(call.arguments as RemoteMessage); + await _onPushBackgroundMessage(call.arguments as RemoteMessage); + return; default: throw PlatformException( code: 'invalid_method', message: 'No such method ${call.method}'); From 4822735ce348869401c3d01101eb12209e8d2a40 Mon Sep 17 00:00:00 2001 From: Derek Date: Thu, 30 Nov 2023 17:44:34 -0500 Subject: [PATCH 2/7] Update push_notification_events_internal.dart I changed handleBackgroundMessage() to become a Future and async/await. --- lib/src/platform/src/push_notification_events_internal.dart | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/src/platform/src/push_notification_events_internal.dart b/lib/src/platform/src/push_notification_events_internal.dart index 9782cc740..4b6363d38 100644 --- a/lib/src/platform/src/push_notification_events_internal.dart +++ b/lib/src/platform/src/push_notification_events_internal.dart @@ -81,9 +81,9 @@ class PushNotificationEventsInternal implements PushNotificationEvents { /// @nodoc /// Handles a RemoteMessage passed from the platform side. - void handleBackgroundMessage(RemoteMessage remoteMessage) { + Future handleBackgroundMessage(RemoteMessage remoteMessage) async { if (_onBackgroundMessage != null) { - _onBackgroundMessage!(remoteMessage); + await _onBackgroundMessage!(remoteMessage); } else { // ignore:avoid_print print('Received RemoteMessage but no handler was set. ' From 2b328394ea671350a385fd68cc95e25aaa4e23b3 Mon Sep 17 00:00:00 2001 From: Derek Date: Thu, 30 Nov 2023 17:47:36 -0500 Subject: [PATCH 3/7] Update push_notification_events.dart I made the BackgroundMessageHandler a Future instead of void so that we can async/await for it to be completed. --- lib/src/push_notifications/src/push_notification_events.dart | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/src/push_notifications/src/push_notification_events.dart b/lib/src/push_notifications/src/push_notification_events.dart index bdd939ed8..60abacdc0 100644 --- a/lib/src/push_notifications/src/push_notification_events.dart +++ b/lib/src/push_notifications/src/push_notification_events.dart @@ -4,7 +4,7 @@ import 'package:ably_flutter/ably_flutter.dart'; /// Type definition for a callback invoked when background push message /// is received. -typedef BackgroundMessageHandler = void Function(RemoteMessage message); +typedef BackgroundMessageHandler = Future Function(RemoteMessage message); /// Push Notification events, such as message arriving or notification tap. abstract class PushNotificationEvents { From 5fb7e6e8baabb224357e8e0ffecffcd6db90f431 Mon Sep 17 00:00:00 2001 From: Derek Date: Tue, 5 Dec 2023 14:24:50 -0500 Subject: [PATCH 4/7] Update push_notification_events_internal.dart handleBackgroundMessage now handles when _onBackgroundMessage is defined as void or Future It checks to see if it's a Future and then awaits it if it is. --- lib/src/platform/src/push_notification_events_internal.dart | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/lib/src/platform/src/push_notification_events_internal.dart b/lib/src/platform/src/push_notification_events_internal.dart index 4b6363d38..c5ec7b187 100644 --- a/lib/src/platform/src/push_notification_events_internal.dart +++ b/lib/src/platform/src/push_notification_events_internal.dart @@ -83,7 +83,11 @@ class PushNotificationEventsInternal implements PushNotificationEvents { /// Handles a RemoteMessage passed from the platform side. Future handleBackgroundMessage(RemoteMessage remoteMessage) async { if (_onBackgroundMessage != null) { - await _onBackgroundMessage!(remoteMessage); + dynamic onBackgroundMessageResult = _onBackgroundMessage!(remoteMessage); + // ^^^ _onBackgroundMessage() can return void or Future + if (onBackgroundMessageResult is Future) { + await onBackgroundMessageResult; // if it returns a Future we await on it. + } } else { // ignore:avoid_print print('Received RemoteMessage but no handler was set. ' From f8f92dd093bfd3ec962904e228a3f042edf4e2f8 Mon Sep 17 00:00:00 2001 From: Derek Date: Tue, 5 Dec 2023 14:26:10 -0500 Subject: [PATCH 5/7] Update push_notification_events.dart Changed the cast on BackgroundMessageHandler to dynamic, this allows it to be defined as both void and Future --- lib/src/push_notifications/src/push_notification_events.dart | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/src/push_notifications/src/push_notification_events.dart b/lib/src/push_notifications/src/push_notification_events.dart index 60abacdc0..f36ff76ea 100644 --- a/lib/src/push_notifications/src/push_notification_events.dart +++ b/lib/src/push_notifications/src/push_notification_events.dart @@ -4,7 +4,7 @@ import 'package:ably_flutter/ably_flutter.dart'; /// Type definition for a callback invoked when background push message /// is received. -typedef BackgroundMessageHandler = Future Function(RemoteMessage message); +typedef BackgroundMessageHandler = dynamic Function(RemoteMessage message); /// Push Notification events, such as message arriving or notification tap. abstract class PushNotificationEvents { From 8f678023111e3660b7234799c14f79e5391be6e7 Mon Sep 17 00:00:00 2001 From: eliotRosewater Date: Wed, 6 Dec 2023 13:10:14 -0500 Subject: [PATCH 6/7] Inside of setupCallHandler() changed it to return from await. --- .../background_android_isolate_platform.dart | 21 +++++++------------ 1 file changed, 7 insertions(+), 14 deletions(-) diff --git a/lib/src/platform/src/background_android_isolate_platform.dart b/lib/src/platform/src/background_android_isolate_platform.dart index 5563fe0ff..66ed831cb 100644 --- a/lib/src/platform/src/background_android_isolate_platform.dart +++ b/lib/src/platform/src/background_android_isolate_platform.dart @@ -20,17 +20,14 @@ class BackgroundIsolateAndroidPlatform { _methodChannel.setMethodCallHandler((call) async { switch (call.method) { case PlatformMethod.pushOnBackgroundMessage: - await _onPushBackgroundMessage(call.arguments as RemoteMessage); - return; + return _onPushBackgroundMessage(call.arguments as RemoteMessage); default: - throw PlatformException( - code: 'invalid_method', message: 'No such method ${call.method}'); + throw PlatformException(code: 'invalid_method', message: 'No such method ${call.method}'); } }); } - static final BackgroundIsolateAndroidPlatform _platform = - BackgroundIsolateAndroidPlatform._internal(); + static final BackgroundIsolateAndroidPlatform _platform = BackgroundIsolateAndroidPlatform._internal(); /// @nodoc /// Singleton instance of BackgroundIsolateAndroidPlatform @@ -40,19 +37,15 @@ class BackgroundIsolateAndroidPlatform { /// A method channel used to communicate with the user's app isolate /// we explicitly launched when a RemoteMessage is received. /// Used only on Android. - final MethodChannel _methodChannel = MethodChannel( - 'io.ably.flutter.plugin.background', StandardMethodCodec(Codec())); + final MethodChannel _methodChannel = MethodChannel('io.ably.flutter.plugin.background', StandardMethodCodec(Codec())); - final PushNotificationEventsInternal _pushNotificationEvents = - Push.notificationEvents as PushNotificationEventsInternal; + final PushNotificationEventsInternal _pushNotificationEvents = Push.notificationEvents as PushNotificationEventsInternal; - Future _onPushBackgroundMessage(RemoteMessage remoteMessage) async => - _pushNotificationEvents.handleBackgroundMessage(remoteMessage); + Future _onPushBackgroundMessage(RemoteMessage remoteMessage) async => _pushNotificationEvents.handleBackgroundMessage(remoteMessage); /// @nodoc /// Equivalent of [Platform.invokePlatformMethod] which cannot be used here /// because we may not be able to acquire [Platform] instance here, so we /// need to use a raw [MethodChannel] communication - Future invokeMethod(String method, [dynamic arguments]) async => - _methodChannel.invokeMethod(method, arguments); + Future invokeMethod(String method, [dynamic arguments]) async => _methodChannel.invokeMethod(method, arguments); } From 997c3f268def53990fb10b76cd3ecb82dd937dfd Mon Sep 17 00:00:00 2001 From: evgeny Date: Wed, 13 Dec 2023 17:13:02 +0000 Subject: [PATCH 7/7] fix: formatting --- .../background_android_isolate_platform.dart | 18 ++++++++++++------ lib/src/platform/src/method_call_handler.dart | 2 +- .../src/push_notification_events_internal.dart | 7 ++++--- 3 files changed, 17 insertions(+), 10 deletions(-) diff --git a/lib/src/platform/src/background_android_isolate_platform.dart b/lib/src/platform/src/background_android_isolate_platform.dart index 66ed831cb..cf743b3b5 100644 --- a/lib/src/platform/src/background_android_isolate_platform.dart +++ b/lib/src/platform/src/background_android_isolate_platform.dart @@ -22,12 +22,14 @@ class BackgroundIsolateAndroidPlatform { case PlatformMethod.pushOnBackgroundMessage: return _onPushBackgroundMessage(call.arguments as RemoteMessage); default: - throw PlatformException(code: 'invalid_method', message: 'No such method ${call.method}'); + throw PlatformException( + code: 'invalid_method', message: 'No such method ${call.method}'); } }); } - static final BackgroundIsolateAndroidPlatform _platform = BackgroundIsolateAndroidPlatform._internal(); + static final BackgroundIsolateAndroidPlatform _platform = + BackgroundIsolateAndroidPlatform._internal(); /// @nodoc /// Singleton instance of BackgroundIsolateAndroidPlatform @@ -37,15 +39,19 @@ class BackgroundIsolateAndroidPlatform { /// A method channel used to communicate with the user's app isolate /// we explicitly launched when a RemoteMessage is received. /// Used only on Android. - final MethodChannel _methodChannel = MethodChannel('io.ably.flutter.plugin.background', StandardMethodCodec(Codec())); + final MethodChannel _methodChannel = MethodChannel( + 'io.ably.flutter.plugin.background', StandardMethodCodec(Codec())); - final PushNotificationEventsInternal _pushNotificationEvents = Push.notificationEvents as PushNotificationEventsInternal; + final PushNotificationEventsInternal _pushNotificationEvents = + Push.notificationEvents as PushNotificationEventsInternal; - Future _onPushBackgroundMessage(RemoteMessage remoteMessage) async => _pushNotificationEvents.handleBackgroundMessage(remoteMessage); + Future _onPushBackgroundMessage(RemoteMessage remoteMessage) async => + _pushNotificationEvents.handleBackgroundMessage(remoteMessage); /// @nodoc /// Equivalent of [Platform.invokePlatformMethod] which cannot be used here /// because we may not be able to acquire [Platform] instance here, so we /// need to use a raw [MethodChannel] communication - Future invokeMethod(String method, [dynamic arguments]) async => _methodChannel.invokeMethod(method, arguments); + Future invokeMethod(String method, [dynamic arguments]) async => + _methodChannel.invokeMethod(method, arguments); } diff --git a/lib/src/platform/src/method_call_handler.dart b/lib/src/platform/src/method_call_handler.dart index 566ee9f0f..ac05625d8 100644 --- a/lib/src/platform/src/method_call_handler.dart +++ b/lib/src/platform/src/method_call_handler.dart @@ -94,7 +94,7 @@ class AblyMethodCallHandler { } Future _onPushBackgroundMessage(RemoteMessage remoteMessage) async { - _pushNotificationEvents.handleBackgroundMessage(remoteMessage); + await _pushNotificationEvents.handleBackgroundMessage(remoteMessage); return null; } diff --git a/lib/src/platform/src/push_notification_events_internal.dart b/lib/src/platform/src/push_notification_events_internal.dart index c5ec7b187..a00e42193 100644 --- a/lib/src/platform/src/push_notification_events_internal.dart +++ b/lib/src/platform/src/push_notification_events_internal.dart @@ -83,10 +83,11 @@ class PushNotificationEventsInternal implements PushNotificationEvents { /// Handles a RemoteMessage passed from the platform side. Future handleBackgroundMessage(RemoteMessage remoteMessage) async { if (_onBackgroundMessage != null) { - dynamic onBackgroundMessageResult = _onBackgroundMessage!(remoteMessage); + final onBackgroundMessageResult = _onBackgroundMessage!(remoteMessage); // ^^^ _onBackgroundMessage() can return void or Future - if (onBackgroundMessageResult is Future) { - await onBackgroundMessageResult; // if it returns a Future we await on it. + if (onBackgroundMessageResult is Future) { + // if it returns a Future we await on it. + await onBackgroundMessageResult; } } else { // ignore:avoid_print