Skip to content

Commit

Permalink
fixup! TF-3157 Remove unnecessary background service worker
Browse files Browse the repository at this point in the history
  • Loading branch information
tddang-linagora committed Nov 5, 2024
1 parent e197413 commit 89b6fc4
Show file tree
Hide file tree
Showing 3 changed files with 2 additions and 50 deletions.
1 change: 0 additions & 1 deletion core/lib/data/constants/constant.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,4 @@ class Constant {
static const octetStreamMimeType = 'application/octet-stream';
static const pdfExtension = '.pdf';
static const imageType = 'image';
static const wsServiceWorkerBroadcastChannel = 'background-message';
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
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: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 show MessageEvent, DomException;

@pragma('vm:entry-point')
Future<void> handleFirebaseBackgroundMessage(RemoteMessage message) async {
Expand All @@ -23,32 +19,11 @@ class FcmReceiver {

static const int MAX_COUNT_RETRY_TO_GET_FCM_TOKEN = 3;

int _countRetryToGetFcmToken = 0;

Future onInitialFcmListener() async {
_countRetryToGetFcmToken = 0;
_onForegroundMessage();
_onBackgroundMessage();

if (PlatformInfo.isWeb) {
_onMessageBroadcastChannel();
await _requestNotificationPermissionOnWeb();
} else {
await _onHandleFcmToken();
}
}

Future<void> _requestNotificationPermissionOnWeb() async {
NotificationSettings notificationSetting = await FirebaseMessaging.instance.getNotificationSettings();
log('FcmReceiver::_requestNotificationPermissionOnWeb: authorizationStatus = ${notificationSetting.authorizationStatus}');
if (notificationSetting.authorizationStatus != AuthorizationStatus.authorized) {
notificationSetting = await FirebaseMessaging.instance.requestPermission();
if (notificationSetting.authorizationStatus == AuthorizationStatus.authorized) {
await _onHandleFcmToken();
}
} else {
await _onHandleFcmToken();
}
await _onHandleFcmToken();
}

void _onForegroundMessage() {
Expand All @@ -59,38 +34,17 @@ class FcmReceiver {
FirebaseMessaging.onBackgroundMessage(handleFirebaseBackgroundMessage);
}

void _onMessageBroadcastChannel() {
final broadcast = BroadcastChannel('background-message');
broadcast.onMessage.listen((event) {
if (event is html.MessageEvent) {
FcmService.instance.handleMessageEventBroadcastChannel(event);
}
});
}

Future<String?> _getInitialToken() async {
try {
final vapidKey = PlatformInfo.isWeb ? AppConfig.fcmVapidPublicKeyWeb : null;
final token = await FirebaseMessaging.instance.getToken(vapidKey: vapidKey);
final token = await FirebaseMessaging.instance.getToken(vapidKey: null);
log('FcmReceiver::_getInitialToken:token: $token');
return token;
} catch (e) {
logError('FcmReceiver::_getInitialToken: TYPE = ${e.runtimeType} | Exception = $e');
if (PlatformInfo.isWeb
&& e is html.DomException
&& _countRetryToGetFcmToken < MAX_COUNT_RETRY_TO_GET_FCM_TOKEN) {
return await _retryGetToken();
}
return null;
}
}

Future<String?> _retryGetToken() async {
_countRetryToGetFcmToken++;
log('FcmReceiver::_retryGetToken: CountRetry = $_countRetryToGetFcmToken');
return await _getInitialToken();
}

Future _onHandleFcmToken() async {
final token = await _getInitialToken();
FcmService.instance.handleToken(token);
Expand Down
1 change: 0 additions & 1 deletion lib/main/utils/app_config.dart
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ class AppConfig {
return supportedOtherPlatform == 'supported';
}
}
static String get fcmVapidPublicKeyWeb => dotenv.get('FIREBASE_WEB_VAPID_PUBLIC_KEY', fallback: '');
static List<String> get oidcScopes {
try {
final envScopes = dotenv.get('OIDC_SCOPES', fallback: '');
Expand Down

0 comments on commit 89b6fc4

Please sign in to comment.