Skip to content

Commit

Permalink
TF-2298 Always get token when open app
Browse files Browse the repository at this point in the history
Signed-off-by: dab246 <[email protected]>
  • Loading branch information
dab246 authored and hoangdat committed Dec 22, 2023
1 parent 3fc44a3 commit fae6398
Show file tree
Hide file tree
Showing 10 changed files with 105 additions and 148 deletions.
11 changes: 8 additions & 3 deletions lib/features/base/base_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,9 @@ import 'package:tmail_ui_user/features/push_notification/domain/usecases/get_sto
import 'package:tmail_ui_user/features/push_notification/presentation/bindings/fcm_interactor_bindings.dart';
import 'package:tmail_ui_user/features/push_notification/presentation/config/fcm_configuration.dart';
import 'package:tmail_ui_user/features/push_notification/presentation/controller/fcm_message_controller.dart';
import 'package:tmail_ui_user/features/push_notification/presentation/controller/fcm_token_controller.dart';
import 'package:tmail_ui_user/features/push_notification/presentation/services/fcm_receiver.dart';
import 'package:tmail_ui_user/features/push_notification/presentation/services/fcm_service.dart';
import 'package:tmail_ui_user/main/bindings/network/binding_tag.dart';
import 'package:tmail_ui_user/main/error/capability_validator.dart';
import 'package:tmail_ui_user/main/exceptions/remote_exception.dart';
Expand Down Expand Up @@ -268,7 +270,10 @@ abstract class BaseController extends GetxController
await AppUtils.loadFcmConfigFileToEnv(currentMapEnvData: mapEnvData);
await FcmConfiguration.initialize();
FcmInteractorBindings().dependencies();
FcmMessageController.instance.initializeFromAccountId(accountId, session);
FcmService.instance.initialStreamController();
FcmMessageController.instance.initialize(accountId: accountId, session: session);
FcmTokenController.instance.initialBindingInteractor();
await FcmReceiver.instance.onInitialFcmListener();
} else {
throw NotSupportFCMException();
}
Expand Down Expand Up @@ -355,7 +360,7 @@ abstract class BaseController extends GetxController
authorizationInterceptors.clear();
authorizationIsolateInterceptors.clear();
if (_isFcmEnabled) {
_fcmReceiver.deleteFcmToken();
await _fcmReceiver.deleteFcmToken();
}
await cachingManager.closeHive();
}
Expand All @@ -372,7 +377,7 @@ abstract class BaseController extends GetxController
authorizationIsolateInterceptors.clear();
authorizationInterceptors.clear();
if (_isFcmEnabled) {
_fcmReceiver.deleteFcmToken();
await _fcmReceiver.deleteFcmToken();
}
await cachingManager.closeHive();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ import 'package:tmail_ui_user/features/email/domain/usecases/mark_as_email_read_
import 'package:tmail_ui_user/features/email/domain/usecases/mark_as_star_email_interactor.dart';
import 'package:tmail_ui_user/features/email/domain/usecases/move_to_mailbox_interactor.dart';
import 'package:tmail_ui_user/features/email/domain/usecases/unsubscribe_email_interactor.dart';
import 'package:tmail_ui_user/features/email/presentation/controller/email_supervisor_controller.dart';
import 'package:tmail_ui_user/features/email/presentation/controller/single_email_controller.dart';
import 'package:tmail_ui_user/features/email/presentation/bindings/email_bindings.dart';
import 'package:tmail_ui_user/features/home/domain/repository/session_repository.dart';
import 'package:tmail_ui_user/features/home/domain/usecases/store_session_interactor.dart';
Expand All @@ -42,7 +40,6 @@ import 'package:tmail_ui_user/features/mailbox/data/repository/mailbox_repositor
import 'package:tmail_ui_user/features/mailbox/domain/repository/mailbox_repository.dart';
import 'package:tmail_ui_user/features/mailbox/domain/usecases/mark_as_mailbox_read_interactor.dart';
import 'package:tmail_ui_user/features/mailbox/presentation/mailbox_bindings.dart';
import 'package:tmail_ui_user/features/mailbox/presentation/mailbox_controller.dart';
import 'package:tmail_ui_user/features/mailbox_dashboard/data/datasource/search_datasource.dart';
import 'package:tmail_ui_user/features/mailbox_dashboard/data/datasource/session_storage_composer_datasource.dart';
import 'package:tmail_ui_user/features/mailbox_dashboard/data/datasource/spam_report_datasource.dart';
Expand Down Expand Up @@ -92,7 +89,6 @@ import 'package:tmail_ui_user/features/sending_queue/domain/usecases/store_sendi
import 'package:tmail_ui_user/features/sending_queue/domain/usecases/update_sending_email_interactor.dart';
import 'package:tmail_ui_user/features/sending_queue/presentation/bindings/sending_queue_bindings.dart';
import 'package:tmail_ui_user/features/sending_queue/presentation/bindings/sending_queue_interactor_bindings.dart';
import 'package:tmail_ui_user/features/sending_queue/presentation/sending_queue_controller.dart';
import 'package:tmail_ui_user/features/thread/data/datasource/thread_datasource.dart';
import 'package:tmail_ui_user/features/thread/data/datasource_impl/local_thread_datasource_impl.dart';
import 'package:tmail_ui_user/features/thread/data/datasource_impl/thread_datasource_impl.dart';
Expand All @@ -110,7 +106,6 @@ import 'package:tmail_ui_user/features/thread/domain/usecases/move_multiple_emai
import 'package:tmail_ui_user/features/thread/domain/usecases/search_email_interactor.dart';
import 'package:tmail_ui_user/features/thread/domain/usecases/search_more_email_interactor.dart';
import 'package:tmail_ui_user/features/thread/presentation/thread_bindings.dart';
import 'package:tmail_ui_user/features/thread/presentation/thread_controller.dart';
import 'package:tmail_ui_user/main/exceptions/cache_exception_thrower.dart';
import 'package:tmail_ui_user/main/exceptions/remote_exception_thrower.dart';

Expand Down Expand Up @@ -356,13 +351,4 @@ class MailboxDashBoardBindings extends BaseBindings {
},
));
}

void deleteController() {
Get.delete<MailboxController>();
Get.delete<ThreadController>();
Get.delete<SingleEmailController>();
Get.delete<EmailSupervisorController>();
Get.delete<DownloadController>();
Get.delete<SendingQueueController>();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ import 'package:tmail_ui_user/features/mailbox_dashboard/domain/state/remove_ema
import 'package:tmail_ui_user/features/mailbox_dashboard/domain/usecases/get_composer_cache_on_web_interactor.dart';
import 'package:tmail_ui_user/features/mailbox_dashboard/domain/usecases/remove_email_drafts_interactor.dart';
import 'package:tmail_ui_user/features/mailbox_dashboard/presentation/action/dashboard_action.dart';
import 'package:tmail_ui_user/features/mailbox_dashboard/presentation/bindings/mailbox_dashboard_bindings.dart';
import 'package:tmail_ui_user/features/mailbox_dashboard/presentation/controller/app_grid_dashboard_controller.dart';
import 'package:tmail_ui_user/features/mailbox_dashboard/presentation/controller/download/download_controller.dart';
import 'package:tmail_ui_user/features/mailbox_dashboard/presentation/controller/search_controller.dart' as search;
Expand Down Expand Up @@ -2406,7 +2405,6 @@ class MailboxDashBoardController extends ReloadableController {
_notificationManager.closeStream();
_fcmService.closeStream();
BackButtonInterceptor.removeByName(AppRoutes.dashboard);
MailboxDashBoardBindings().deleteController();
super.onClose();
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import 'package:fcm/model/type_name.dart';
import 'package:jmap_dart_client/jmap/account_id.dart';
import 'package:jmap_dart_client/jmap/core/state.dart' as jmap;
import 'package:jmap_dart_client/jmap/core/user_name.dart';
import 'package:model/extensions/account_id_extensions.dart';
import 'package:tmail_ui_user/features/caching/clients/fcm_cache_client.dart';
import 'package:tmail_ui_user/features/caching/clients/firebase_registration_cache_client.dart';
import 'package:tmail_ui_user/features/caching/utils/cache_utils.dart';
import 'package:tmail_ui_user/features/push_notification/data/model/firebase_registration_cache.dart';
import 'package:tmail_ui_user/features/push_notification/domain/exceptions/fcm_exception.dart';
import 'package:jmap_dart_client/jmap/core/state.dart' as jmap;

class FCMCacheManager {
final FcmCacheClient _fcmCacheClient;
Expand Down Expand Up @@ -62,4 +62,11 @@ class FCMCacheManager {
Future<void> deleteFirebaseRegistration() async {
await _firebaseRegistrationCacheClient.deleteItem(FirebaseRegistrationCache.keyCacheValue);
}

Future<void> closeCacheBox() async {
await Future.wait([
_fcmCacheClient.closeBox(),
_firebaseRegistrationCacheClient.closeBox(),
]);
}
}
Original file line number Diff line number Diff line change
@@ -1,22 +1,9 @@

import 'package:firebase_core/firebase_core.dart';
import 'package:tmail_ui_user/features/push_notification/presentation/config/firebase_options.dart';
import 'package:tmail_ui_user/features/push_notification/presentation/services/fcm_receiver.dart';
import 'package:tmail_ui_user/features/push_notification/presentation/services/fcm_service.dart';

class FcmConfiguration {

static Future<void> initialize() async {
await Firebase.initializeApp(options: DefaultFirebaseOptions.currentPlatform);
await FcmService.instance.recreateStreamController();
_initMessageListener();
}

static void _initMessageListener() {
FcmReceiver.instance.onForegroundMessage();
FcmReceiver.instance.onBackgroundMessage();
FcmReceiver.instance.onMessageOpenedApp();
FcmReceiver.instance.getFcmToken();
FcmReceiver.instance.onRefreshFcmToken();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import 'package:tmail_ui_user/features/login/domain/state/get_stored_token_oidc_
import 'package:tmail_ui_user/features/login/domain/usecases/get_authenticated_account_interactor.dart';
import 'package:tmail_ui_user/features/mailbox_dashboard/presentation/bindings/mailbox_dashboard_bindings.dart';
import 'package:tmail_ui_user/features/offline_mode/manager/new_email_cache_manager.dart';
import 'package:tmail_ui_user/features/push_notification/data/local/fcm_cache_manager.dart';
import 'package:tmail_ui_user/features/push_notification/presentation/action/fcm_action.dart';
import 'package:tmail_ui_user/features/push_notification/presentation/bindings/fcm_interactor_bindings.dart';
import 'package:tmail_ui_user/features/push_notification/presentation/controller/fcm_base_controller.dart';
Expand All @@ -52,51 +53,43 @@ class FcmMessageController extends FcmBaseController {
AuthorizationInterceptors? _authorizationInterceptors;
GetSessionInteractor? _getSessionInteractor;
NewEmailCacheManager? _newEmailCacheManager;
FCMCacheManager? _fcmCacheManager;

FcmMessageController._internal() {
_listenFcmStream();
}
FcmMessageController._internal();

static final FcmMessageController _instance = FcmMessageController._internal();

static FcmMessageController get instance => _instance;

void initializeFromAccountId(AccountId accountId, Session session) {
void initialize({AccountId? accountId, Session? session}) {
_currentAccountId = accountId;
_currentSession = session;
_userName = session.username;
FcmTokenController.instance.initialize();
}
_userName = session?.username;

void initialize() {}

void _listenFcmStream() async {
await Future.wait([
listenForegroundMessageStream(),
listenBackgroundMessageStream(),
listenTokenStream()
]);
_listenTokenStream();
_listenForegroundMessageStream();
_listenBackgroundMessageStream();
}

Future<void> listenForegroundMessageStream() {
FcmService.instance.foregroundMessageStream
void _listenForegroundMessageStream() {
FcmService.instance.foregroundMessageStreamController
?.stream
.throttleTime(const Duration(milliseconds: FcmService.durationMessageComing))
.listen(_handleForegroundMessageAction);
return Future.value();
}

Future<void> listenBackgroundMessageStream() {
FcmService.instance.backgroundMessageStream
void _listenBackgroundMessageStream() {
FcmService.instance.backgroundMessageStreamController
?.stream
.throttleTime(const Duration(milliseconds: FcmService.durationMessageComing))
.listen(_handleBackgroundMessageAction);
return Future.value();
}

Future<void> listenTokenStream() {
FcmService.instance.fcmTokenStream
.debounceTime(const Duration(milliseconds: FcmService.durationRefreshToken))
void _listenTokenStream() {
FcmService.instance.fcmTokenStreamController
?.stream
.throttleTime(const Duration(milliseconds: FcmService.durationRefreshToken))
.listen(FcmTokenController.instance.onFcmTokenChanged);
return Future.value();
}

void _handleForegroundMessageAction(RemoteMessage newRemoteMessage) {
Expand Down Expand Up @@ -201,7 +194,12 @@ class FcmMessageController extends FcmBaseController {

_getInteractorBindings();

await _newEmailCacheManager?.closeNewEmailHiveCacheBox();
await Future.wait([
if (_newEmailCacheManager != null)
_newEmailCacheManager!.closeNewEmailHiveCacheBox(),
if (_fcmCacheManager != null)
_fcmCacheManager!.closeCacheBox(),
]);
}

void _getInteractorBindings() {
Expand All @@ -210,7 +208,9 @@ class FcmMessageController extends FcmBaseController {
_authorizationInterceptors = getBinding<AuthorizationInterceptors>();
_getSessionInteractor = getBinding<GetSessionInteractor>();
_newEmailCacheManager = getBinding<NewEmailCacheManager>();
FcmTokenController.instance.initialize();
_fcmCacheManager = getBinding<FCMCacheManager>();

FcmTokenController.instance.initialBindingInteractor();
}

void _getAuthenticatedAccount() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class FcmTokenController extends FcmBaseController {
UpdateFirebaseRegistrationTokenInteractor? _updateFirebaseRegistrationTokenInteractor;
DeleteFirebaseRegistrationCacheInteractor? _deleteFirebaseRegistrationCacheInteractor;

void initialize() {
void initialBindingInteractor() {
_storeFirebaseRegistrationInteractor = getBinding<StoreFirebaseRegistrationInteractor>();
_getFirebaseRegistrationByDeviceIdInteractor = getBinding<GetFirebaseRegistrationByDeviceIdInteractor>();
_registerNewFirebaseRegistrationTokenInteractor = getBinding<RegisterNewFirebaseRegistrationTokenInteractor>();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@

import 'package:core/utils/app_logger.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/features/push_notification/presentation/utils/fcm_utils.dart';
import 'package:tmail_ui_user/main/utils/app_utils.dart';
import 'package:tmail_ui_user/main/utils/app_config.dart';

@pragma('vm:entry-point')
Future<void> handleFirebaseBackgroundMessage(RemoteMessage message) async {
log('FcmReceiver::handleFirebaseBackgroundMessage(): ${message.data}');
FcmService.instance.initialStreamController();
FcmMessageController.instance.initialize();
FcmService.instance.handleFirebaseBackgroundMessage(message);
}

Expand All @@ -18,35 +20,48 @@ class FcmReceiver {

static FcmReceiver get instance => _instance;

void onForegroundMessage() {
Future onInitialFcmListener() async {
log('FcmReceiver::onInitialFcmListener:');
await _onHandleFcmToken();

_onForegroundMessage();
_onBackgroundMessage();
_onMessageOpenedApp();
}

void _onForegroundMessage() {
FirebaseMessaging.onMessage.listen(FcmService.instance.handleFirebaseForegroundMessage);
}

void onBackgroundMessage() {
void _onBackgroundMessage() {
FirebaseMessaging.onBackgroundMessage(handleFirebaseBackgroundMessage);
}

void onMessageOpenedApp() {
void _onMessageOpenedApp() {
FirebaseMessaging.onMessageOpenedApp.listen(FcmService.instance.handleFirebaseMessageOpenedApp);
}

void getFcmToken() async {
try {
final currentToken = await FirebaseMessaging.instance.getToken(vapidKey: AppUtils.fcmVapidPublicKey);
log('FcmReceiver::onFcmToken():currentToken: $currentToken');
if (!FcmUtils.instance.isMobileAndroid) {
FcmService.instance.handleGetToken(currentToken);
}
} catch(e) {
log('FcmReceiver::onFcmToken():exception: $e');
}
Future<String?> _getInitialToken() async {
final token = await FirebaseMessaging.instance.getToken(
vapidKey: PlatformInfo.isWeb ? AppConfig.fcmVapidPublicKeyWeb : null
);
log('FcmReceiver::_getInitialToken:token: $token');
return token;
}

void onRefreshFcmToken() {
FirebaseMessaging.instance.onTokenRefresh.listen(FcmService.instance.handleRefreshToken);
Future _onHandleFcmToken() async {
final token = await _getInitialToken();
FcmService.instance.handleToken(token);

FirebaseMessaging.instance.onTokenRefresh.listen((newToken) {
log('FcmReceiver::_onHandleFcmToken:onTokenRefresh: $newToken');
if (newToken != token) {
FcmService.instance.handleToken(newToken);
}
});
}
void deleteFcmToken(){
FirebaseMessaging.instance.deleteToken();

Future deleteFcmToken() async {
await FirebaseMessaging.instance.deleteToken();
}
}
Loading

0 comments on commit fae6398

Please sign in to comment.