From f5e6cd08221720d657816f27e73f191f55ab5be6 Mon Sep 17 00:00:00 2001 From: dab246 Date: Mon, 25 Mar 2024 09:36:13 +0700 Subject: [PATCH] TF-2460 Use conditional export for BroadcastChannel to avoid failed compilation --- core/lib/core.dart | 1 + .../utils/broadcast_channel/broadcast_channel.dart | 1 + .../broadcast_channel/broadcast_channel_stub.dart | 7 +++++++ .../broadcast_channel/broadcast_channel_web.dart | 1 + .../presentation/services/fcm_receiver.dart | 11 ++++++++--- 5 files changed, 18 insertions(+), 3 deletions(-) create mode 100644 core/lib/utils/broadcast_channel/broadcast_channel.dart create mode 100644 core/lib/utils/broadcast_channel/broadcast_channel_stub.dart create mode 100644 core/lib/utils/broadcast_channel/broadcast_channel_web.dart diff --git a/core/lib/core.dart b/core/lib/core.dart index b22f23e44..596285f9c 100644 --- a/core/lib/core.dart +++ b/core/lib/core.dart @@ -44,6 +44,7 @@ export 'utils/platform_info.dart'; export 'utils/file_utils.dart'; export 'utils/option_param_mixin.dart'; export 'utils/print_utils.dart'; +export 'utils/broadcast_channel/broadcast_channel.dart'; // Views export 'presentation/views/text/slogan_builder.dart'; diff --git a/core/lib/utils/broadcast_channel/broadcast_channel.dart b/core/lib/utils/broadcast_channel/broadcast_channel.dart new file mode 100644 index 000000000..6ab539e56 --- /dev/null +++ b/core/lib/utils/broadcast_channel/broadcast_channel.dart @@ -0,0 +1 @@ +export 'broadcast_channel_stub.dart' if (dart.library.html) 'broadcast_channel_web.dart'; \ No newline at end of file diff --git a/core/lib/utils/broadcast_channel/broadcast_channel_stub.dart b/core/lib/utils/broadcast_channel/broadcast_channel_stub.dart new file mode 100644 index 000000000..0bea5d512 --- /dev/null +++ b/core/lib/utils/broadcast_channel/broadcast_channel_stub.dart @@ -0,0 +1,7 @@ +class BroadcastChannel { + final String name; + + BroadcastChannel(this.name); + + Stream get onMessage => const Stream.empty(); +} \ No newline at end of file diff --git a/core/lib/utils/broadcast_channel/broadcast_channel_web.dart b/core/lib/utils/broadcast_channel/broadcast_channel_web.dart new file mode 100644 index 000000000..4235926e2 --- /dev/null +++ b/core/lib/utils/broadcast_channel/broadcast_channel_web.dart @@ -0,0 +1 @@ +export 'dart:html' show BroadcastChannel; \ No newline at end of file diff --git a/lib/features/push_notification/presentation/services/fcm_receiver.dart b/lib/features/push_notification/presentation/services/fcm_receiver.dart index c5a8d493d..082d8f3b9 100644 --- a/lib/features/push_notification/presentation/services/fcm_receiver.dart +++ b/lib/features/push_notification/presentation/services/fcm_receiver.dart @@ -1,12 +1,13 @@ import 'package:core/utils/app_logger.dart'; +import 'package:core/utils/broadcast_channel/broadcast_channel.dart'; import 'package:core/utils/platform_info.dart'; import 'package:firebase_messaging/firebase_messaging.dart'; import 'package:flutter/services.dart'; import 'package:tmail_ui_user/features/push_notification/presentation/controller/fcm_message_controller.dart'; import 'package:tmail_ui_user/features/push_notification/presentation/services/fcm_service.dart'; import 'package:tmail_ui_user/main/utils/app_config.dart'; -import 'package:universal_html/html.dart' as html; +import 'package:universal_html/html.dart' as html show MessageEvent; @pragma('vm:entry-point') Future handleFirebaseBackgroundMessage(RemoteMessage message) async { @@ -54,8 +55,12 @@ class FcmReceiver { } void _onMessageBroadcastChannel() { - final broadcast = html.BroadcastChannel('background-message'); - broadcast.onMessage.listen(FcmService.instance.handleMessageEventBroadcastChannel); + final broadcast = BroadcastChannel('background-message'); + broadcast.onMessage.listen((event) { + if (event is html.MessageEvent) { + FcmService.instance.handleMessageEventBroadcastChannel(event); + } + }); } Future _getInitialToken() async {