TF-2384 Store data to keychain when save new Account
Signed-off-by: dab246 <tdvu@linagora.com> (cherry picked from commit 4d84a3354927d1c0becaf0e8e6d7cc8c54721f58)
This commit is contained in:
@@ -2,7 +2,6 @@ import 'package:core/data/model/source_type/data_source_type.dart';
|
||||
import 'package:core/utils/file_utils.dart';
|
||||
import 'package:get/get.dart';
|
||||
import 'package:tmail_ui_user/features/base/interactors_bindings.dart';
|
||||
import 'package:tmail_ui_user/features/caching/clients/state_cache_client.dart';
|
||||
import 'package:tmail_ui_user/features/email/data/datasource/email_datasource.dart';
|
||||
import 'package:tmail_ui_user/features/email/data/datasource/html_datasource.dart';
|
||||
import 'package:tmail_ui_user/features/email/data/datasource_impl/email_datasource_impl.dart';
|
||||
@@ -20,6 +19,7 @@ import 'package:tmail_ui_user/features/mailbox/data/datasource_impl/mailbox_cach
|
||||
import 'package:tmail_ui_user/features/mailbox/data/datasource_impl/mailbox_datasource_impl.dart';
|
||||
import 'package:tmail_ui_user/features/mailbox/data/datasource_impl/state_datasource_impl.dart';
|
||||
import 'package:tmail_ui_user/features/mailbox/data/local/mailbox_cache_manager.dart';
|
||||
import 'package:tmail_ui_user/features/mailbox/data/local/state_cache_manager.dart';
|
||||
import 'package:tmail_ui_user/features/mailbox/data/network/mailbox_api.dart';
|
||||
import 'package:tmail_ui_user/features/mailbox/data/network/mailbox_isolate_worker.dart';
|
||||
import 'package:tmail_ui_user/features/offline_mode/manager/new_email_cache_manager.dart';
|
||||
@@ -28,8 +28,8 @@ import 'package:tmail_ui_user/features/offline_mode/manager/opened_email_cache_m
|
||||
import 'package:tmail_ui_user/features/offline_mode/manager/opened_email_cache_worker_queue.dart';
|
||||
import 'package:tmail_ui_user/features/offline_mode/manager/sending_email_cache_manager.dart';
|
||||
import 'package:tmail_ui_user/features/push_notification/data/datasource/fcm_datasource.dart';
|
||||
import 'package:tmail_ui_user/features/push_notification/data/datasource_impl/fcm_datasource_impl.dart';
|
||||
import 'package:tmail_ui_user/features/push_notification/data/datasource_impl/cache_fcm_datasource_impl.dart';
|
||||
import 'package:tmail_ui_user/features/push_notification/data/datasource_impl/fcm_datasource_impl.dart';
|
||||
import 'package:tmail_ui_user/features/push_notification/data/local/fcm_cache_manager.dart';
|
||||
import 'package:tmail_ui_user/features/push_notification/data/network/fcm_api.dart';
|
||||
import 'package:tmail_ui_user/features/push_notification/data/repository/fcm_repository_impl.dart';
|
||||
@@ -101,8 +101,9 @@ class FcmInteractorBindings extends InteractorsBindings {
|
||||
Get.find<HtmlAnalyzer>(),
|
||||
Get.find<RemoteExceptionThrower>()));
|
||||
Get.lazyPut(() => StateDataSourceImpl(
|
||||
Get.find<StateCacheClient>(),
|
||||
Get.find<CacheExceptionThrower>()));
|
||||
Get.find<StateCacheManager>(),
|
||||
Get.find<CacheExceptionThrower>()
|
||||
));
|
||||
Get.lazyPut(() => EmailHiveCacheDataSourceImpl(
|
||||
Get.find<NewEmailCacheManager>(),
|
||||
Get.find<OpenedEmailCacheManager>(),
|
||||
|
||||
+24
-25
@@ -6,14 +6,13 @@ import 'package:core/data/network/config/dynamic_url_interceptors.dart';
|
||||
import 'package:core/presentation/state/failure.dart';
|
||||
import 'package:core/presentation/state/success.dart';
|
||||
import 'package:core/utils/app_logger.dart';
|
||||
import 'package:core/utils/platform_info.dart';
|
||||
import 'package:fcm/model/type_name.dart';
|
||||
import 'package:firebase_messaging/firebase_messaging.dart';
|
||||
import 'package:jmap_dart_client/jmap/account_id.dart';
|
||||
import 'package:jmap_dart_client/jmap/core/session/session.dart';
|
||||
import 'package:jmap_dart_client/jmap/core/state.dart' as jmap;
|
||||
import 'package:jmap_dart_client/jmap/core/user_name.dart';
|
||||
import 'package:jmap_dart_client/jmap/push/state_change.dart';
|
||||
import 'package:model/oidc/token_oidc.dart';
|
||||
import 'package:rxdart/rxdart.dart';
|
||||
import 'package:tmail_ui_user/features/base/action/ui_action.dart';
|
||||
import 'package:tmail_ui_user/features/caching/config/hive_cache_config.dart';
|
||||
@@ -46,7 +45,7 @@ class FcmMessageController extends FcmBaseController {
|
||||
AccountId? _currentAccountId;
|
||||
Session? _currentSession;
|
||||
UserName? _userName;
|
||||
RemoteMessage? _remoteMessageBackground;
|
||||
Map<String, dynamic>? _payloadData;
|
||||
|
||||
GetAuthenticatedAccountInteractor? _getAuthenticatedAccountInteractor;
|
||||
DynamicUrlInterceptors? _dynamicUrlInterceptors;
|
||||
@@ -92,24 +91,24 @@ class FcmMessageController extends FcmBaseController {
|
||||
.listen(FcmTokenController.instance.onFcmTokenChanged);
|
||||
}
|
||||
|
||||
void _handleForegroundMessageAction(RemoteMessage newRemoteMessage) {
|
||||
log('FcmMessageController::_handleForegroundMessageAction():remoteMessage: ${newRemoteMessage.data} | _currentAccountId: $_currentAccountId');
|
||||
void _handleForegroundMessageAction(Map<String, dynamic> payloadData) {
|
||||
log('FcmMessageController::_handleForegroundMessageAction():payloadData: $payloadData | _currentAccountId: $_currentAccountId');
|
||||
if (_currentAccountId != null && _userName != null) {
|
||||
final stateChange = _convertRemoteMessageToStateChange(newRemoteMessage);
|
||||
final stateChange = _parsingPayloadData(payloadData);
|
||||
final mapTypeState = stateChange.getMapTypeState(_currentAccountId!);
|
||||
_mappingTypeStateToAction(mapTypeState, _currentAccountId!, _userName!, session: _currentSession);
|
||||
}
|
||||
}
|
||||
|
||||
void _handleBackgroundMessageAction(RemoteMessage newRemoteMessage) async {
|
||||
log('FcmMessageController::_handleBackgroundMessageAction():remoteMessage: ${newRemoteMessage.data}');
|
||||
_remoteMessageBackground = newRemoteMessage;
|
||||
void _handleBackgroundMessageAction(Map<String, dynamic> payloadData) async {
|
||||
log('FcmMessageController::_handleBackgroundMessageAction():payloadData: $payloadData');
|
||||
_payloadData = payloadData;
|
||||
await _initialAppConfig();
|
||||
_getAuthenticatedAccount();
|
||||
}
|
||||
|
||||
StateChange? _convertRemoteMessageToStateChange(RemoteMessage newRemoteMessage) {
|
||||
return FcmUtils.instance.convertFirebaseDataMessageToStateChange(newRemoteMessage.data);
|
||||
StateChange? _parsingPayloadData(Map<String, dynamic> payloadData) {
|
||||
return FcmUtils.instance.convertFirebaseDataMessageToStateChange(payloadData);
|
||||
}
|
||||
|
||||
void _mappingTypeStateToAction(
|
||||
@@ -217,7 +216,7 @@ class FcmMessageController extends FcmBaseController {
|
||||
if (_getAuthenticatedAccountInteractor != null) {
|
||||
consumeState(_getAuthenticatedAccountInteractor!.execute());
|
||||
} else {
|
||||
_clearRemoteMessageBackground();
|
||||
_clearPayloadData();
|
||||
logError('FcmMessageController::_getAuthenticatedAccount():_getAuthenticatedAccountInteractor is null');
|
||||
}
|
||||
}
|
||||
@@ -225,7 +224,7 @@ class FcmMessageController extends FcmBaseController {
|
||||
void _handleGetAuthenticatedAccountSuccess(GetAuthenticatedAccountSuccess success) {
|
||||
_currentAccountId = success.account.accountId;
|
||||
_userName = success.account.userName;
|
||||
if (!FcmUtils.instance.isMobileAndroid) {
|
||||
if (!PlatformInfo.isAndroid) {
|
||||
_dynamicUrlInterceptors?.changeBaseUrl(success.account.apiUrl);
|
||||
}
|
||||
log('FcmMessageController::_handleGetAuthenticatedAccountSuccess():_currentAccountId: $_currentAccountId | _userName: $_userName');
|
||||
@@ -235,11 +234,11 @@ class FcmMessageController extends FcmBaseController {
|
||||
log('FcmMessageController::_handleGetAccountByOidcSuccess():');
|
||||
_dynamicUrlInterceptors?.setJmapUrl(storedTokenOidcSuccess.baseUrl.toString());
|
||||
_authorizationInterceptors?.setTokenAndAuthorityOidc(
|
||||
newToken: storedTokenOidcSuccess.tokenOidc.toToken(),
|
||||
newToken: storedTokenOidcSuccess.tokenOidc,
|
||||
newConfig: storedTokenOidcSuccess.oidcConfiguration
|
||||
);
|
||||
|
||||
if (FcmUtils.instance.isMobileAndroid) {
|
||||
if (PlatformInfo.isAndroid) {
|
||||
_dynamicUrlInterceptors?.changeBaseUrl(storedTokenOidcSuccess.baseUrl.toString());
|
||||
_getSessionAction();
|
||||
} else {
|
||||
@@ -254,7 +253,7 @@ class FcmMessageController extends FcmBaseController {
|
||||
credentialViewState.userName,
|
||||
credentialViewState.password,
|
||||
);
|
||||
if (FcmUtils.instance.isMobileAndroid) {
|
||||
if (PlatformInfo.isAndroid) {
|
||||
_dynamicUrlInterceptors?.changeBaseUrl(credentialViewState.baseUrl.toString());
|
||||
_getSessionAction();
|
||||
} else {
|
||||
@@ -266,7 +265,7 @@ class FcmMessageController extends FcmBaseController {
|
||||
if (_getSessionInteractor != null) {
|
||||
consumeState(_getSessionInteractor!.execute());
|
||||
} else {
|
||||
_clearRemoteMessageBackground();
|
||||
_clearPayloadData();
|
||||
logError('FcmMessageController::_getSessionAction():_getSessionInteractor is null');
|
||||
}
|
||||
}
|
||||
@@ -280,29 +279,29 @@ class FcmMessageController extends FcmBaseController {
|
||||
_dynamicUrlInterceptors?.changeBaseUrl(apiUrl);
|
||||
_pushActionFromRemoteMessageBackground();
|
||||
} else {
|
||||
_clearRemoteMessageBackground();
|
||||
_clearPayloadData();
|
||||
logError('FcmMessageController::_handleGetSessionSuccess():apiUrl is null');
|
||||
}
|
||||
}
|
||||
|
||||
void _pushActionFromRemoteMessageBackground() {
|
||||
log('FcmMessageController::_pushActionFromRemoteMessageBackground():_remoteMessageBackground: $_remoteMessageBackground | _currentAccountId: $_currentAccountId | _currentSession: $_currentSession');
|
||||
if (_remoteMessageBackground != null && _currentAccountId != null && _userName != null) {
|
||||
final stateChange = _convertRemoteMessageToStateChange(_remoteMessageBackground!);
|
||||
log('FcmMessageController::_pushActionFromRemoteMessageBackground():_payloadData: $_payloadData | _currentAccountId: $_currentAccountId | _currentSession: $_currentSession');
|
||||
if (_payloadData?.isNotEmpty == true && _currentAccountId != null && _userName != null) {
|
||||
final stateChange = _parsingPayloadData(_payloadData!);
|
||||
final mapTypeState = stateChange.getMapTypeState(_currentAccountId!);
|
||||
_mappingTypeStateToAction(mapTypeState, _currentAccountId!, _userName!, isForeground: false, session: _currentSession);
|
||||
}
|
||||
_clearRemoteMessageBackground();
|
||||
_clearPayloadData();
|
||||
}
|
||||
|
||||
void _clearRemoteMessageBackground() {
|
||||
_remoteMessageBackground = null;
|
||||
void _clearPayloadData() {
|
||||
_payloadData = null;
|
||||
}
|
||||
|
||||
@override
|
||||
void handleFailureViewState(Failure failure) {
|
||||
log('FcmMessageController::_handleFailureViewState(): $failure');
|
||||
_clearRemoteMessageBackground();
|
||||
_clearPayloadData();
|
||||
}
|
||||
|
||||
@override
|
||||
|
||||
@@ -1,6 +1,4 @@
|
||||
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:core/data/network/config/dynamic_url_interceptors.dart';
|
||||
import 'package:core/presentation/state/failure.dart';
|
||||
import 'package:core/presentation/state/success.dart';
|
||||
@@ -46,13 +44,9 @@ import 'package:tmail_ui_user/features/push_notification/domain/usecases/store_e
|
||||
import 'package:tmail_ui_user/features/push_notification/domain/usecases/store_email_state_to_refresh_interactor.dart';
|
||||
import 'package:tmail_ui_user/features/push_notification/presentation/action/fcm_action.dart';
|
||||
import 'package:tmail_ui_user/features/push_notification/presentation/listener/change_listener.dart';
|
||||
import 'package:tmail_ui_user/features/push_notification/presentation/notification/local_notification_config.dart';
|
||||
import 'package:tmail_ui_user/features/push_notification/presentation/notification/local_notification_manager.dart';
|
||||
import 'package:tmail_ui_user/features/push_notification/presentation/utils/fcm_utils.dart';
|
||||
import 'package:tmail_ui_user/features/thread/domain/constants/thread_constants.dart';
|
||||
import 'package:tmail_ui_user/main/localizations/app_localizations.dart';
|
||||
import 'package:tmail_ui_user/main/routes/route_navigation.dart';
|
||||
import 'package:uuid/uuid.dart';
|
||||
|
||||
class EmailChangeListener extends ChangeListener {
|
||||
|
||||
@@ -103,7 +97,7 @@ class EmailChangeListener extends ChangeListener {
|
||||
log('EmailChangeListener::dispatchActions():actions: $actions');
|
||||
for (var action in actions) {
|
||||
if (action is SynchronizeEmailOnForegroundAction) {
|
||||
if (FcmUtils.instance.isMobileAndroid) {
|
||||
if (PlatformInfo.isAndroid) {
|
||||
_handleRemoveNotificationWhenEmailMarkAsRead(action.newState, action.accountId, action.session);
|
||||
}
|
||||
_synchronizeEmailOnForegroundAction(action.newState);
|
||||
@@ -113,11 +107,11 @@ class EmailChangeListener extends ChangeListener {
|
||||
} else if (action is PushNotificationAction) {
|
||||
_pushNotificationAction(action.newState, action.accountId, action.userName, action.session);
|
||||
|
||||
if (FcmUtils.instance.isMobileAndroid) {
|
||||
if (PlatformInfo.isAndroid) {
|
||||
_getNewReceiveEmailFromNotificationAction(action.session, action.accountId, action.newState);
|
||||
}
|
||||
} else if (action is StoreEmailStateToRefreshAction) {
|
||||
if (FcmUtils.instance.isMobileAndroid) {
|
||||
if (PlatformInfo.isAndroid) {
|
||||
_handleRemoveNotificationWhenEmailMarkAsRead(action.newState, action.accountId, action.session);
|
||||
}
|
||||
_handleStoreEmailStateToRefreshAction(action.accountId, action.userName, action.newState);
|
||||
@@ -141,32 +135,20 @@ class EmailChangeListener extends ChangeListener {
|
||||
|
||||
if (PlatformInfo.isWeb) {
|
||||
_storeEmailDeliveryStateAction(accountId, userName, _newStateEmailDelivery!);
|
||||
} else {
|
||||
if (Platform.isAndroid) {
|
||||
_getStoredEmailDeliveryState(accountId, userName);
|
||||
} else if (Platform.isIOS) {
|
||||
_storeEmailDeliveryStateAction(accountId, userName, _newStateEmailDelivery!);
|
||||
_showLocalNotificationForIOS(_newStateEmailDelivery!, accountId);
|
||||
} else {
|
||||
logError('EmailChangeListener::_pushNotificationAction(): NOT SUPPORTED PLATFORM');
|
||||
}
|
||||
} else if (PlatformInfo.isAndroid) {
|
||||
_getStoredEmailDeliveryState(accountId, userName);
|
||||
}
|
||||
}
|
||||
|
||||
void _getStoredEmailDeliveryState(AccountId accountId, UserName userName) {
|
||||
if (_getStoredEmailDeliveryStateInteractor != null) {
|
||||
consumeState(_getStoredEmailDeliveryStateInteractor!.execute(accountId, userName));
|
||||
} else {
|
||||
_showDefaultLocalNotification();
|
||||
}
|
||||
}
|
||||
|
||||
void _getStoredEmailState() {
|
||||
if (_getStoredEmailStateInteractor != null && _session != null && _accountId != null) {
|
||||
consumeState(_getStoredEmailStateInteractor!.execute(_session!, _accountId!));
|
||||
} else {
|
||||
_showDefaultLocalNotification();
|
||||
logError('EmailChangeListener::_getStoredEmailState(): _getStoredEmailStateInteractor is null');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -183,16 +165,12 @@ class EmailChangeListener extends ChangeListener {
|
||||
propertiesCreated: EmailUtils.getPropertiesForEmailGetMethod(_session!, _accountId!),
|
||||
propertiesUpdated: ThreadConstants.propertiesUpdatedDefault,
|
||||
));
|
||||
} else {
|
||||
_showDefaultLocalNotification();
|
||||
}
|
||||
}
|
||||
|
||||
void _storeEmailDeliveryStateAction(AccountId accountId, UserName userName, jmap.State state) {
|
||||
if (_storeEmailDeliveryStateInteractor != null) {
|
||||
consumeState(_storeEmailDeliveryStateInteractor!.execute(accountId, userName, state));
|
||||
} else {
|
||||
_showDefaultLocalNotification();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -208,48 +186,12 @@ class EmailChangeListener extends ChangeListener {
|
||||
);
|
||||
}
|
||||
|
||||
void _showLocalNotificationForIOS(jmap.State newState, AccountId accountId) async {
|
||||
final notificationPayload = NotificationPayload(newState: newState);
|
||||
log('EmailChangeListener::_showLocalNotificationForIOS():notificationPayload: $notificationPayload');
|
||||
LocalNotificationManager.instance.showPushNotification(
|
||||
id: '${FcmUtils.instance.platformOS}-${accountId.id.value}',
|
||||
title: currentContext != null
|
||||
? AppLocalizations.of(currentContext!).appTitlePushNotification
|
||||
: LocalNotificationConfig.notificationTitle,
|
||||
message: currentContext != null
|
||||
? AppLocalizations.of(currentContext!).youHaveNewMessages
|
||||
: LocalNotificationConfig.notificationMessage,
|
||||
payload: notificationPayload.encodeToString,
|
||||
);
|
||||
await LocalNotificationManager.instance.setNotificationBadgeForIOS();
|
||||
}
|
||||
|
||||
void _showDefaultLocalNotification() {
|
||||
LocalNotificationManager.instance.showPushNotification(
|
||||
id: '${FcmUtils.instance.platformOS}-${const Uuid().v1()}',
|
||||
title: currentContext != null
|
||||
? AppLocalizations.of(currentContext!).appTitlePushNotification
|
||||
: LocalNotificationConfig.notificationTitle,
|
||||
message: currentContext != null
|
||||
? AppLocalizations.of(currentContext!).youHaveNewMessages
|
||||
: LocalNotificationConfig.notificationMessage,
|
||||
);
|
||||
|
||||
if (PlatformInfo.isAndroid) {
|
||||
LocalNotificationManager.instance.groupPushNotification();
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
void handleFailureViewState(Failure failure) {
|
||||
log('EmailChangeListener::_handleFailureViewState(): $failure');
|
||||
if (failure is GetStoredEmailDeliveryStateFailure &&
|
||||
failure.exception is NotFoundEmailDeliveryStateException) {
|
||||
_getStoredEmailState();
|
||||
} else if (failure is NotFoundEmailState ||
|
||||
failure is GetStoredEmailStateFailure ||
|
||||
failure is GetEmailChangesToPushNotificationFailure) {
|
||||
_showDefaultLocalNotification();
|
||||
} else if (failure is GetMailboxesNotPutNotificationsFailure) {
|
||||
final listEmails = _emailsAvailablePushNotification.toEmailsAvailablePushNotification();
|
||||
_handleLocalPushNotification(listEmails);
|
||||
@@ -259,25 +201,15 @@ class EmailChangeListener extends ChangeListener {
|
||||
@override
|
||||
void handleSuccessViewState(Success success) {
|
||||
log('EmailChangeListener::_handleSuccessViewState(): $success');
|
||||
if (success is GetStoredEmailDeliveryStateSuccess) {
|
||||
if (_newStateEmailDelivery != success.state) {
|
||||
_getEmailChangesAction(success.state);
|
||||
} else {
|
||||
_showDefaultLocalNotification();
|
||||
}
|
||||
if (success is GetStoredEmailDeliveryStateSuccess && _newStateEmailDelivery != success.state) {
|
||||
_getEmailChangesAction(success.state);
|
||||
} else if (success is GetStoredEmailStateSuccess) {
|
||||
_getEmailChangesAction(success.state);
|
||||
} else if (success is GetEmailChangesToPushNotificationSuccess) {
|
||||
if (_newStateEmailDelivery != null) {
|
||||
_storeEmailDeliveryStateAction(success.accountId, success.userName, _newStateEmailDelivery!);
|
||||
} else if (success is GetEmailChangesToPushNotificationSuccess && _newStateEmailDelivery != null) {
|
||||
_storeEmailDeliveryStateAction(success.accountId, success.userName, _newStateEmailDelivery!);
|
||||
|
||||
if (FcmUtils.instance.isMobileAndroid) {
|
||||
_handleListEmailToPushNotification(success.emailList);
|
||||
} else {
|
||||
_showDefaultLocalNotification();
|
||||
}
|
||||
} else {
|
||||
_showDefaultLocalNotification();
|
||||
if (PlatformInfo.isAndroid) {
|
||||
_handleListEmailToPushNotification(success.emailList);
|
||||
}
|
||||
} else if (success is GetMailboxesNotPutNotificationsSuccess) {
|
||||
final listEmails = _emailsAvailablePushNotification.toEmailsAvailablePushNotification(
|
||||
@@ -308,7 +240,6 @@ class EmailChangeListener extends ChangeListener {
|
||||
void _handleLocalPushNotification(List<PresentationEmail> emailList) {
|
||||
log('EmailChangeListener::_handleLocalPushNotification():emailList: $emailList');
|
||||
if (emailList.isEmpty) {
|
||||
_showDefaultLocalNotification();
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
+12
-31
@@ -1,16 +1,13 @@
|
||||
|
||||
import 'dart:async';
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:core/presentation/extensions/html_extension.dart';
|
||||
import 'package:core/utils/app_logger.dart';
|
||||
import 'package:core/utils/platform_info.dart';
|
||||
import 'package:flutter_app_badger/flutter_app_badger.dart';
|
||||
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
|
||||
import 'package:jmap_dart_client/jmap/mail/email/email_address.dart';
|
||||
import 'package:model/extensions/email_address_extension.dart';
|
||||
import 'package:tmail_ui_user/features/push_notification/presentation/notification/local_notification_config.dart';
|
||||
import 'package:tmail_ui_user/features/push_notification/presentation/utils/fcm_utils.dart';
|
||||
import 'package:tmail_ui_user/main/localizations/app_localizations.dart';
|
||||
import 'package:tmail_ui_user/main/routes/route_navigation.dart';
|
||||
|
||||
@@ -38,15 +35,14 @@ class LocalNotificationManager {
|
||||
Future<void> setUp() async {
|
||||
try {
|
||||
await _initLocalNotification();
|
||||
_checkLocalNotificationPermission();
|
||||
if (Platform.isAndroid) {
|
||||
await _checkLocalNotificationPermission();
|
||||
if (PlatformInfo.isAndroid) {
|
||||
await _createAndroidNotificationChannelGroup();
|
||||
await _createAndroidNotificationChannel();
|
||||
}
|
||||
} catch (e) {
|
||||
logError('LocalNotificationManager::setUp(): ERROR: ${e.toString()}');
|
||||
}
|
||||
return Future.value();
|
||||
}
|
||||
|
||||
Future<NotificationResponse?> getCurrentNotificationResponse() async {
|
||||
@@ -56,7 +52,7 @@ class LocalNotificationManager {
|
||||
} catch (e) {
|
||||
logError('LocalNotificationManager::getCurrentNotificationResponse(): ERROR: ${e.toString()}');
|
||||
}
|
||||
return Future.value(null);
|
||||
return null;
|
||||
}
|
||||
|
||||
set activatedNotificationClickedOnTerminate(bool clicked) => _isNotificationClickedOnTerminate = clicked;
|
||||
@@ -82,19 +78,19 @@ class LocalNotificationManager {
|
||||
}
|
||||
}
|
||||
|
||||
void _checkLocalNotificationPermission() async {
|
||||
Future<void> _checkLocalNotificationPermission() async {
|
||||
if (_notificationsEnabled) {
|
||||
return;
|
||||
return Future.value(null);
|
||||
}
|
||||
|
||||
if (Platform.isAndroid) {
|
||||
if (PlatformInfo.isAndroid) {
|
||||
final granted = await _isAndroidPermissionGranted();
|
||||
if (!granted) {
|
||||
_notificationsEnabled = await _requestPermissions();
|
||||
} else {
|
||||
_notificationsEnabled = granted;
|
||||
}
|
||||
} else {
|
||||
} else if (PlatformInfo.isIOS) {
|
||||
_notificationsEnabled = await _requestPermissions();
|
||||
}
|
||||
}
|
||||
@@ -106,14 +102,16 @@ class LocalNotificationManager {
|
||||
}
|
||||
|
||||
Future<bool> _requestPermissions() async {
|
||||
if (Platform.isAndroid) {
|
||||
if (PlatformInfo.isAndroid) {
|
||||
return await _localNotificationsPlugin
|
||||
.resolvePlatformSpecificImplementation<AndroidFlutterLocalNotificationsPlugin>()
|
||||
?.requestPermission() ?? false;
|
||||
} else {
|
||||
} else if (PlatformInfo.isIOS) {
|
||||
return await _localNotificationsPlugin
|
||||
.resolvePlatformSpecificImplementation<IOSFlutterLocalNotificationsPlugin>()
|
||||
?.requestPermissions(alert: true, badge: true, sound: true) ?? false;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -166,14 +164,11 @@ class LocalNotificationManager {
|
||||
}
|
||||
|
||||
Future<void> removeNotification(String id) async {
|
||||
if (id.startsWith(FcmUtils.instance.platformOS)) {
|
||||
await removeNotificationBadgeForIOS();
|
||||
}
|
||||
return _localNotificationsPlugin.cancel(id.hashCode);
|
||||
}
|
||||
|
||||
void groupPushNotification() async {
|
||||
if (Platform.isIOS) {
|
||||
if (PlatformInfo.isIOS) {
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -223,18 +218,4 @@ class LocalNotificationManager {
|
||||
void closeStream() {
|
||||
localNotificationsController.close();
|
||||
}
|
||||
|
||||
Future<void> setNotificationBadgeForIOS() async {
|
||||
log("LocalNotificationManager::setNotificationBadgeForIOS:");
|
||||
if (PlatformInfo.isIOS) {
|
||||
await FlutterAppBadger.updateBadgeCount(1);
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> removeNotificationBadgeForIOS() async {
|
||||
log("LocalNotificationManager::removeNotificationBadgeForIOS:");
|
||||
if (PlatformInfo.isIOS) {
|
||||
await FlutterAppBadger.removeBadge();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -26,7 +26,6 @@ class FcmReceiver {
|
||||
|
||||
_onForegroundMessage();
|
||||
_onBackgroundMessage();
|
||||
_onMessageOpenedApp();
|
||||
}
|
||||
|
||||
void _onForegroundMessage() {
|
||||
@@ -37,10 +36,6 @@ class FcmReceiver {
|
||||
FirebaseMessaging.onBackgroundMessage(handleFirebaseBackgroundMessage);
|
||||
}
|
||||
|
||||
void _onMessageOpenedApp() {
|
||||
FirebaseMessaging.onMessageOpenedApp.listen(FcmService.instance.handleFirebaseMessageOpenedApp);
|
||||
}
|
||||
|
||||
Future<String?> _getInitialToken() async {
|
||||
final token = await FirebaseMessaging.instance.getToken(
|
||||
vapidKey: PlatformInfo.isWeb ? AppConfig.fcmVapidPublicKeyWeb : null
|
||||
|
||||
@@ -3,15 +3,14 @@ import 'dart:async';
|
||||
|
||||
import 'package:core/utils/app_logger.dart';
|
||||
import 'package:firebase_messaging/firebase_messaging.dart';
|
||||
import 'package:tmail_ui_user/features/push_notification/presentation/notification/local_notification_manager.dart';
|
||||
|
||||
class FcmService {
|
||||
|
||||
static const int durationMessageComing = 2000;
|
||||
static const int durationRefreshToken = 2000;
|
||||
|
||||
StreamController<RemoteMessage>? foregroundMessageStreamController;
|
||||
StreamController<RemoteMessage>? backgroundMessageStreamController;
|
||||
StreamController<Map<String, dynamic>>? foregroundMessageStreamController;
|
||||
StreamController<Map<String, dynamic>>? backgroundMessageStreamController;
|
||||
StreamController<String?>? fcmTokenStreamController;
|
||||
|
||||
FcmService._internal();
|
||||
@@ -22,17 +21,16 @@ class FcmService {
|
||||
|
||||
void handleFirebaseForegroundMessage(RemoteMessage newRemoteMessage) {
|
||||
log('FcmService::handleFirebaseForegroundMessage():data: ${newRemoteMessage.data}');
|
||||
foregroundMessageStreamController?.add(newRemoteMessage);
|
||||
if (newRemoteMessage.data.isNotEmpty) {
|
||||
foregroundMessageStreamController?.add(newRemoteMessage.data);
|
||||
}
|
||||
}
|
||||
|
||||
void handleFirebaseBackgroundMessage(RemoteMessage newRemoteMessage) {
|
||||
log('FcmService::handleFirebaseBackgroundMessage():data: ${newRemoteMessage.data}');
|
||||
backgroundMessageStreamController?.add(newRemoteMessage);
|
||||
}
|
||||
|
||||
void handleFirebaseMessageOpenedApp(RemoteMessage newRemoteMessage) async {
|
||||
log("FcmService::handleFirebaseMessageOpenedApp:");
|
||||
await LocalNotificationManager.instance.removeNotificationBadgeForIOS();
|
||||
if (newRemoteMessage.data.isNotEmpty) {
|
||||
backgroundMessageStreamController?.add(newRemoteMessage.data);
|
||||
}
|
||||
}
|
||||
|
||||
void handleToken(String? token) {
|
||||
@@ -42,8 +40,8 @@ class FcmService {
|
||||
|
||||
void initialStreamController() {
|
||||
log('FcmService::initialStreamController:');
|
||||
foregroundMessageStreamController = StreamController<RemoteMessage>.broadcast();
|
||||
backgroundMessageStreamController = StreamController<RemoteMessage>.broadcast();
|
||||
foregroundMessageStreamController = StreamController<Map<String, dynamic>>.broadcast();
|
||||
backgroundMessageStreamController = StreamController<Map<String, dynamic>>.broadcast();
|
||||
fcmTokenStreamController = StreamController<String?>.broadcast();
|
||||
}
|
||||
|
||||
|
||||
@@ -1,6 +1,4 @@
|
||||
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:core/domain/extensions/datetime_extension.dart';
|
||||
import 'package:core/utils/app_logger.dart';
|
||||
import 'package:core/utils/platform_info.dart';
|
||||
@@ -94,38 +92,13 @@ class FcmUtils {
|
||||
return object == null || (object is String && object.isEmpty);
|
||||
}
|
||||
|
||||
String get platformOS {
|
||||
var platformName = '';
|
||||
if (PlatformInfo.isWeb) {
|
||||
platformName = 'Web';
|
||||
} else {
|
||||
if (Platform.isAndroid) {
|
||||
platformName = 'Android';
|
||||
} else if (Platform.isIOS) {
|
||||
platformName = 'IOS';
|
||||
} else if (Platform.isFuchsia) {
|
||||
platformName = 'Fuchsia';
|
||||
} else if (Platform.isLinux) {
|
||||
platformName = 'Linux';
|
||||
} else if (Platform.isMacOS) {
|
||||
platformName = 'MacOS';
|
||||
} else if (Platform.isWindows) {
|
||||
platformName = 'Windows';
|
||||
}
|
||||
}
|
||||
log('FcmUtils::platformOS():$platformName');
|
||||
return platformName;
|
||||
}
|
||||
|
||||
String hashTokenToDeviceId(String token) {
|
||||
final tokenHashCode = token.hashCode.toString();
|
||||
final deviceId = '$hashCodeKey-$platformOS-$tokenHashCode';
|
||||
final deviceId = '$hashCodeKey-${PlatformInfo.platformNameOS}-$tokenHashCode';
|
||||
log('FcmUtils::hashCodeTokenToDeviceId():deviceId: $deviceId');
|
||||
return deviceId;
|
||||
}
|
||||
|
||||
bool get isMobileAndroid => PlatformInfo.isMobile && Platform.isAndroid;
|
||||
|
||||
bool checkExpirationTimeWithinGivenPeriod({
|
||||
required DateTime expiredTime,
|
||||
required DateTime currentTime,
|
||||
|
||||
Reference in New Issue
Block a user