From fb796e47a5d797fa7612e4569d608f0ee8f3bedc Mon Sep 17 00:00:00 2001 From: dab246 Date: Fri, 23 Jan 2026 11:12:53 +0700 Subject: [PATCH] TF-4268 Add a log to monitor push notifications on Android. --- lib/features/base/base_controller.dart | 8 ++- .../get_stored_email_state_interactor.dart | 8 ++- .../extensions/list_mailbox_id_extension.dart | 4 ++ .../mailbox_dashboard_controller.dart | 8 ++- ...irebase_registration_cache_interactor.dart | 8 ++- ...irebase_registration_token_interactor.dart | 8 ++- ...irebase_registration_token_interactor.dart | 8 ++- .../config/fcm_configuration.dart | 8 ++- .../controller/fcm_message_controller.dart | 49 +++++++++++++++++-- .../controller/fcm_token_controller.dart | 20 ++++++++ .../listener/email_change_listener.dart | 37 ++++++++++++++ .../local_notification_manager.dart | 16 ++++-- .../presentation/services/fcm_receiver.dart | 27 +++++++--- 13 files changed, 184 insertions(+), 25 deletions(-) diff --git a/lib/features/base/base_controller.dart b/lib/features/base/base_controller.dart index fabf996a9..6d72f2785 100644 --- a/lib/features/base/base_controller.dart +++ b/lib/features/base/base_controller.dart @@ -374,8 +374,12 @@ abstract class BaseController extends GetxController } else { throw NotSupportFCMException(); } - } catch(e) { - logWarning('$runtimeType::injectFCMBindings(): exception: $e'); + } catch (e, st) { + logError( + '$runtimeType::injectFCMBindings():', + exception: e, + stackTrace: st, + ); } } diff --git a/lib/features/email/domain/usecases/get_stored_email_state_interactor.dart b/lib/features/email/domain/usecases/get_stored_email_state_interactor.dart index 2cdc1bd08..548527e07 100644 --- a/lib/features/email/domain/usecases/get_stored_email_state_interactor.dart +++ b/lib/features/email/domain/usecases/get_stored_email_state_interactor.dart @@ -1,5 +1,6 @@ import 'package:core/presentation/state/failure.dart'; import 'package:core/presentation/state/success.dart'; +import 'package:core/utils/app_logger.dart'; import 'package:dartz/dartz.dart'; import 'package:jmap_dart_client/jmap/account_id.dart'; import 'package:jmap_dart_client/jmap/core/session/session.dart'; @@ -19,7 +20,12 @@ class GetStoredEmailStateInteractor { } else { yield Left(NotFoundEmailState()); } - } catch (e) { + } catch (e, st) { + logError( + 'Get stored email state is failed', + exception: e, + stackTrace: st, + ); yield Left(GetStoredEmailStateFailure(e)); } } diff --git a/lib/features/mailbox/domain/extensions/list_mailbox_id_extension.dart b/lib/features/mailbox/domain/extensions/list_mailbox_id_extension.dart index 0a9631f63..b0ac48505 100644 --- a/lib/features/mailbox/domain/extensions/list_mailbox_id_extension.dart +++ b/lib/features/mailbox/domain/extensions/list_mailbox_id_extension.dart @@ -15,4 +15,8 @@ extension ListMailboxIdExtension on List { }; return maps; } + + List toListString() { + return map((mailboxId) => mailboxId.id.value).toList(); + } } \ No newline at end of file diff --git a/lib/features/mailbox_dashboard/presentation/controller/mailbox_dashboard_controller.dart b/lib/features/mailbox_dashboard/presentation/controller/mailbox_dashboard_controller.dart index e3c574897..f2718615d 100644 --- a/lib/features/mailbox_dashboard/presentation/controller/mailbox_dashboard_controller.dart +++ b/lib/features/mailbox_dashboard/presentation/controller/mailbox_dashboard_controller.dart @@ -862,8 +862,12 @@ class MailboxDashBoardController extends ReloadableController await super.injectFCMBindings(session, accountId); await LocalNotificationManager.instance.recreateStreamController(); _registerLocalNotificationStreamListener(); - } catch (e) { - logWarning('MailboxDashBoardController::injectFCMBindings(): $e'); + } catch (e, st) { + logError( + 'MailboxDashBoardController::injectFCMBindings():', + exception: e, + stackTrace: st, + ); } } diff --git a/lib/features/push_notification/domain/usecases/delete_firebase_registration_cache_interactor.dart b/lib/features/push_notification/domain/usecases/delete_firebase_registration_cache_interactor.dart index af0d180a8..86f0b257c 100644 --- a/lib/features/push_notification/domain/usecases/delete_firebase_registration_cache_interactor.dart +++ b/lib/features/push_notification/domain/usecases/delete_firebase_registration_cache_interactor.dart @@ -1,5 +1,6 @@ import 'package:core/presentation/state/failure.dart'; import 'package:core/presentation/state/success.dart'; +import 'package:core/utils/app_logger.dart'; import 'package:dartz/dartz.dart'; import 'package:tmail_ui_user/features/push_notification/domain/repository/fcm_repository.dart'; import 'package:tmail_ui_user/features/push_notification/domain/state/delete_firebase_registration_cache_state.dart'; @@ -14,7 +15,12 @@ class DeleteFirebaseRegistrationCacheInteractor { yield Right(DeleteFirebaseRegistrationCacheLoading()); await _fcmRepository.deleteFirebaseRegistrationCache(); yield Right(DeleteFirebaseRegistrationCacheSuccess()); - } catch (e) { + } catch (e, st) { + logError( + 'Delete firebase registration cache is failed', + exception: e, + stackTrace: st, + ); yield Left(DeleteFirebaseRegistrationCacheFailure(e)); } } diff --git a/lib/features/push_notification/domain/usecases/register_new_firebase_registration_token_interactor.dart b/lib/features/push_notification/domain/usecases/register_new_firebase_registration_token_interactor.dart index 3e264a94e..c345716f5 100644 --- a/lib/features/push_notification/domain/usecases/register_new_firebase_registration_token_interactor.dart +++ b/lib/features/push_notification/domain/usecases/register_new_firebase_registration_token_interactor.dart @@ -1,6 +1,7 @@ import 'package:core/presentation/state/failure.dart'; import 'package:core/presentation/state/success.dart'; +import 'package:core/utils/app_logger.dart'; import 'package:dartz/dartz.dart'; import 'package:tmail_ui_user/features/push_notification/domain/model/register_new_token_request.dart'; import 'package:tmail_ui_user/features/push_notification/domain/repository/fcm_repository.dart'; @@ -17,7 +18,12 @@ class RegisterNewFirebaseRegistrationTokenInteractor { yield Right(RegisterNewFirebaseRegistrationTokenLoading()); final firebaseRegistration = await _fcmRepository.registerNewFirebaseRegistrationToken(newTokenRequest); yield Right(RegisterNewFirebaseRegistrationTokenSuccess(firebaseRegistration)); - } catch (e) { + } catch (e, st) { + logError( + 'Register new firebase registration token failed', + exception: e, + stackTrace: st, + ); yield Left(RegisterNewFirebaseRegistrationTokenFailure(e)); } } diff --git a/lib/features/push_notification/domain/usecases/update_firebase_registration_token_interactor.dart b/lib/features/push_notification/domain/usecases/update_firebase_registration_token_interactor.dart index 355d9316e..8bb1b305c 100644 --- a/lib/features/push_notification/domain/usecases/update_firebase_registration_token_interactor.dart +++ b/lib/features/push_notification/domain/usecases/update_firebase_registration_token_interactor.dart @@ -1,6 +1,7 @@ import 'package:core/presentation/state/failure.dart'; import 'package:core/presentation/state/success.dart'; +import 'package:core/utils/app_logger.dart'; import 'package:dartz/dartz.dart'; import 'package:tmail_ui_user/features/push_notification/domain/model/update_token_expired_time_request.dart'; import 'package:tmail_ui_user/features/push_notification/domain/repository/fcm_repository.dart'; @@ -17,7 +18,12 @@ class UpdateFirebaseRegistrationTokenInteractor { yield Right(UpdateFirebaseRegistrationTokenLoading()); await _fcmRepository.updateFirebaseRegistrationToken(updateTokenExpiredTimeRequest); yield Right(UpdateFirebaseRegistrationTokenSuccess()); - } catch (e) { + } catch (e, st) { + logError( + 'Update expired time for firebase registration token is failed', + exception: e, + stackTrace: st, + ); yield Left(UpdateFirebaseRegistrationTokenFailure(e)); } } diff --git a/lib/features/push_notification/presentation/config/fcm_configuration.dart b/lib/features/push_notification/presentation/config/fcm_configuration.dart index 11846a85b..1241efcf1 100644 --- a/lib/features/push_notification/presentation/config/fcm_configuration.dart +++ b/lib/features/push_notification/presentation/config/fcm_configuration.dart @@ -7,8 +7,12 @@ class FcmConfiguration { static Future initialize() async { try { await Firebase.initializeApp(options: DefaultFirebaseOptions.currentPlatform); - } catch (e) { - logWarning('FcmConfiguration::initialize: Exception = $e'); + } catch (e, st) { + logError( + 'FcmConfiguration::initialize', + exception: e, + stackTrace: st + ); } } } \ No newline at end of file diff --git a/lib/features/push_notification/presentation/controller/fcm_message_controller.dart b/lib/features/push_notification/presentation/controller/fcm_message_controller.dart index 50eb87ab2..d0d28805a 100644 --- a/lib/features/push_notification/presentation/controller/fcm_message_controller.dart +++ b/lib/features/push_notification/presentation/controller/fcm_message_controller.dart @@ -58,14 +58,32 @@ class FcmMessageController extends PushBaseController { .debounceTime(const Duration( milliseconds: FcmUtils.durationBackgroundMessageComing, )) - .listen(_handleBackgroundMessageAction); + .listen( + _handleBackgroundMessageAction, + onError: (e, st) { + logError( + 'FcmMessageController::_listenBackgroundMessageStream', + exception: e, + stackTrace: st, + ); + }, + ); } void _listenTokenStream() { FcmService.instance.fcmTokenStreamController ?.stream .debounceTime(const Duration(milliseconds: FcmUtils.durationRefreshToken)) - .listen(FcmTokenController.instance.onFcmTokenChanged); + .listen( + FcmTokenController.instance.onFcmTokenChanged, + onError: (e, st) { + logError( + 'FcmMessageController::_listenTokenStream', + exception: e, + stackTrace: st, + ); + }, + ); } void _handleBackgroundMessageAction(Map payloadData) async { @@ -102,6 +120,10 @@ class FcmMessageController extends PushBaseController { void _getAuthenticatedAccount({StateChange? stateChange}) { if (_getAuthenticatedAccountInteractor != null) { consumeState(_getAuthenticatedAccountInteractor!.execute(stateChange: stateChange)); + } else { + logError( + 'GetAuthenticatedAccountInteractor is null', + ); } } @@ -175,9 +197,17 @@ class FcmMessageController extends PushBaseController { userName: success.session.username, stateChange: stateChange, session: success.session); + } else { + logError( + 'FcmMessageController::_handleGetSessionSuccess: Api url or state change is null', + ); } - } catch (e) { - logWarning('FcmMessageController::_handleGetSessionSuccess: Exception $e'); + } catch (e, st) { + logError( + 'FcmMessageController::_handleGetSessionSuccess:', + exception: e, + stackTrace: st, + ); } } @@ -202,6 +232,17 @@ class FcmMessageController extends PushBaseController { @override void handleFailureViewState(Failure failure) { log('FcmMessageController::_handleFailureViewState(): $failure'); + if (failure is GetStoredTokenOidcFailure) { + logError( + 'Get stored token oidc is failed', + exception: failure.exception, + ); + } else if (failure is GetSessionFailure) { + logError( + 'Get session is failed', + exception: failure.exception, + ); + } } @override diff --git a/lib/features/push_notification/presentation/controller/fcm_token_controller.dart b/lib/features/push_notification/presentation/controller/fcm_token_controller.dart index e85e59860..171f1906c 100644 --- a/lib/features/push_notification/presentation/controller/fcm_token_controller.dart +++ b/lib/features/push_notification/presentation/controller/fcm_token_controller.dart @@ -83,12 +83,20 @@ class FcmTokenController extends PushBaseController { void _getStoredFirebaseRegistrationFromCache() { if (_getStoredFirebaseRegistrationInteractor != null) { consumeState(_getStoredFirebaseRegistrationInteractor!.execute()); + } else { + logError( + 'GetStoredFirebaseRegistrationInteractor is null', + ); } } void _storeFirebaseRegistrationToCache(FirebaseRegistration firebaseRegistration){ if (_storeFirebaseRegistrationInteractor != null) { consumeState(_storeFirebaseRegistrationInteractor!.execute(firebaseRegistration)); + } else { + logError( + 'StoreFirebaseRegistrationInteractor is null', + ); } } @@ -123,6 +131,10 @@ class FcmTokenController extends PushBaseController { ) ) ); + } else { + logError( + 'UpdateFirebaseRegistrationTokenInteractor is null or firebaseRegistration.id is null', + ); } } @@ -143,12 +155,20 @@ class FcmTokenController extends PushBaseController { ) ) ); + } else { + logError( + 'RegisterNewFirebaseRegistrationTokenInteractor is null', + ); } } void _deleteFirebaseRegistrationInCache() { if (_deleteFirebaseRegistrationCacheInteractor != null) { consumeState(_deleteFirebaseRegistrationCacheInteractor!.execute()); + } else { + logError( + 'DeleteFirebaseRegistrationCacheInteractor is null', + ); } } diff --git a/lib/features/push_notification/presentation/listener/email_change_listener.dart b/lib/features/push_notification/presentation/listener/email_change_listener.dart index 6bb676dee..20a7606c2 100644 --- a/lib/features/push_notification/presentation/listener/email_change_listener.dart +++ b/lib/features/push_notification/presentation/listener/email_change_listener.dart @@ -26,6 +26,7 @@ import 'package:tmail_ui_user/features/email/domain/usecases/get_stored_email_st import 'package:tmail_ui_user/features/email/domain/usecases/store_list_new_email_interator.dart'; import 'package:tmail_ui_user/features/email/presentation/action/email_ui_action.dart'; import 'package:tmail_ui_user/features/email/presentation/utils/email_utils.dart'; +import 'package:tmail_ui_user/features/mailbox/domain/extensions/list_mailbox_id_extension.dart'; import 'package:tmail_ui_user/features/mailbox_dashboard/presentation/controller/mailbox_dashboard_controller.dart'; import 'package:tmail_ui_user/features/push_notification/domain/exceptions/fcm_exception.dart'; import 'package:tmail_ui_user/features/push_notification/domain/state/get_email_changes_to_push_notification_state.dart'; @@ -138,12 +139,20 @@ class EmailChangeListener extends ChangeListener { void _getStoredEmailDeliveryState(AccountId accountId, UserName userName) { if (_getStoredEmailDeliveryStateInteractor != null) { consumeState(_getStoredEmailDeliveryStateInteractor!.execute(accountId, userName)); + } else { + logError( + 'GetStoredEmailDeliveryStateInteractor is null', + ); } } void _getStoredEmailState() { if (_getStoredEmailStateInteractor != null && _session != null && _accountId != null) { consumeState(_getStoredEmailStateInteractor!.execute(_session!, _accountId!)); + } else { + logError( + 'GetStoredEmailStateInteractor is null', + ); } } @@ -162,12 +171,20 @@ class EmailChangeListener extends ChangeListener { _accountId!, ), )); + } else { + logError( + 'GetEmailChangesToPushNotificationInteractor is null, or accountId is null, or session is null, or userName is null', + ); } } void _storeEmailDeliveryStateAction(AccountId accountId, UserName userName, jmap.State state) { if (_storeEmailDeliveryStateInteractor != null) { consumeState(_storeEmailDeliveryStateInteractor!.execute(accountId, userName, state)); + } else { + logError( + 'StoreEmailDeliveryStateInteractor is null', + ); } } @@ -195,6 +212,11 @@ class EmailChangeListener extends ChangeListener { _getStoredEmailState(); } else if (failure is GetMailboxesNotPutNotificationsFailure) { final listEmails = _emailsAvailablePushNotification.toEmailsAvailablePushNotification(); + if (listEmails.isEmpty) { + logError( + 'List email available to push notification is empty' + ); + } _handleLocalPushNotification( userName: failure.userName, emailList: listEmails @@ -217,10 +239,20 @@ class EmailChangeListener extends ChangeListener { userName: success.userName, emailList: success.emailList ); + if (success.emailList.isEmpty) { + logError( + 'Get new emails with new state ${_newStateEmailDelivery?.value} to push notification is empty' + ); + } } } else if (success is GetMailboxesNotPutNotificationsSuccess) { final listEmails = _emailsAvailablePushNotification.toEmailsAvailablePushNotification( mailboxIdsNotPutNotifications: success.mailboxes.mailboxIds); + if (listEmails.isEmpty) { + logError( + 'List email available to push notification is empty, with mailbox id not put notifications: ${success.mailboxes.mailboxIds.toListString()}' + ); + } _handleLocalPushNotification( userName: success.userName, emailList: listEmails @@ -249,6 +281,11 @@ class EmailChangeListener extends ChangeListener { consumeState(_getMailboxesNotPutNotificationsInteractor!.execute(_session!, _accountId!)); } else { final listEmails = _emailsAvailablePushNotification.toEmailsAvailablePushNotification(); + if (listEmails.isEmpty) { + logError( + 'List email available to push notification is empty' + ); + } _handleLocalPushNotification( userName: userName, emailList: listEmails diff --git a/lib/features/push_notification/presentation/notification/local_notification_manager.dart b/lib/features/push_notification/presentation/notification/local_notification_manager.dart index 54216791f..ae69d8a61 100644 --- a/lib/features/push_notification/presentation/notification/local_notification_manager.dart +++ b/lib/features/push_notification/presentation/notification/local_notification_manager.dart @@ -39,8 +39,12 @@ class LocalNotificationManager { await _createAndroidNotificationChannelGroup(groupId); await _createAndroidNotificationChannel(groupId); } - } catch (e) { - logWarning('LocalNotificationManager::setUp(): ERROR: ${e.toString()}'); + } catch (e, st) { + logError( + 'LocalNotificationManager::setUp:ERROR', + exception: e, + stackTrace: st, + ); } } @@ -48,8 +52,12 @@ class LocalNotificationManager { try { _notificationAppLaunchDetails = await _localNotificationsPlugin.getNotificationAppLaunchDetails(); return _notificationAppLaunchDetails?.notificationResponse; - } catch (e) { - logWarning('LocalNotificationManager::getCurrentNotificationResponse(): ERROR: ${e.toString()}'); + } catch (e, st) { + logError( + 'Get notification app launch details failed from local notification plugin', + exception: e, + stackTrace: st, + ); } return null; } diff --git a/lib/features/push_notification/presentation/services/fcm_receiver.dart b/lib/features/push_notification/presentation/services/fcm_receiver.dart index 109217259..2e72b87c8 100644 --- a/lib/features/push_notification/presentation/services/fcm_receiver.dart +++ b/lib/features/push_notification/presentation/services/fcm_receiver.dart @@ -34,8 +34,12 @@ class FcmReceiver { final token = await FirebaseMessaging.instance.getToken(); log('FcmReceiver::_getInitialToken:token: $token'); return token; - } catch (e) { - logWarning('FcmReceiver::_getInitialToken: TYPE = ${e.runtimeType} | Exception = $e'); + } catch (e, st) { + logError( + 'FcmReceiver::_getInitialToken:', + exception: e, + stackTrace: st, + ); return null; } } @@ -44,11 +48,20 @@ class FcmReceiver { 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); + FirebaseMessaging.instance.onTokenRefresh.listen( + (newToken) { + log('FcmReceiver::_onHandleFcmToken:onTokenRefresh: $newToken'); + if (newToken != token) { + FcmService.instance.handleToken(newToken); + } + }, + onError: (e, st) { + logError( + 'FcmReceiver::_onHandleFcmToken:onTokenRefresh:', + exception: e, + stackTrace: st, + ); } - }); + ); } } \ No newline at end of file